DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jquery實現心算練習代碼
jquery實現心算練習代碼
編輯:JQuery特效代碼     
在線演示:
http://demo.jb51.net/js/jquery_xinsuan/index.htm
看看大家做完要多長時間,代碼如下:
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="zh-CN" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>心算練習,Do Your Best</title>
<style type="text/css">
body
{
text-align: center;
padding: 0;
margin: 0;
}
div
{
width: 1000px;
margin: auto;
}
div table
{
border-collapse: collapse;
width: 100%;
table-layout: fixed;
text-align: left;
}
div table td
{
border: 1px solid silver;
padding-left: 3em;
}
div span
{
padding: 3px 8px;
}
table input
{
width: 3em;
}
.red
{
color: Red;
}
.green
{
color: Green;
}
</style>
<script type="text/javascript" src="http://demo.jb51.net/jslib/jquery/jquery-1.4.4.js"></script>
<script type="text/javascript">

$(function () {
fnInitTable(2);
fnHideResult();
});

//顯示正確結果及分數
function fnShowResult() {
var vCount = 0;
$("table tr td").each(function (i) {
var vUserResult = $.trim($(this).find("input").val());
var vCorrectResult = $.trim($(this).find("span:last-child").text());
if (vUserResult == vCorrectResult) {
vCount++;
$(this).find("span:last-child").show().addClass("green");
}
else {
$(this).find("span:last-child").show().addClass("red");
}
});
$("#score").text(vCount);
}

function fnHideResult() {
$("table td span:last-child").hide();
}

//生成尾數不為零的隨機數
function fnRandomBy(parUnder, parOver) {
var vResult = 0;
while (vResult % 10 == 0) {
switch (arguments.length) {
case 1:
vResult = parseInt(Math.random() * parUnder + 1);
break;
case 2:
vResult = parseInt(Math.random() * (parOver - parUnder + 1) + parUnder);
break;
default:
vResult = 0;
break;
}
}
return vResult;
}

function fnInitTable(parDigit) {
var vPreValue;
var vNextValue;
var vResultt;
$("table").empty();
for (var i = 0; i < 10; i++) {
$("table").append("<tr></tr>");
for (var k = 0; k < 3; k++) {
vPreValue = fnRandomBy(10, 100);
if (parDigit == 2) {
vNextValue = fnRandomBy(10, 100);
}
else {
vNextValue = fnRandomBy(100, 1000);
}
vResultt = vPreValue * vNextValue;
$("table tr:last").append("<td>" + vPreValue + "<span>×</span>" + vNextValue + "<span>=</span>" + "<input type='text' /\>" + "<span>" + vResultt + "</span>" + "</td>");
}
$("table").append("</tr>");
}
fnInit();
}

function fnInit() {
fnResetTime();
fnHideResult();
$("table input").attr("disabled", "true");
$("#score").text("");
}

var vTimerID = 0;
//開始按鈕連續單擊會使得時間加快,vContinueClick可判斷是否連續單擊,多謝小龍女提示
var vContinueClick = 0;
function fnBegin() {
//分數的顯示與否來確認是否單擊結束按鈕
if ($.trim($("#score").text()) != "") {
fnResetTime();
$("table input").val("");
}
fnHideResult();
vContinueClick++;
if (vContinueClick == 1) {
vTimerID = setInterval("fnUpdateTime()", 1000);
}
$("table input").removeAttr("disabled");
}

function fnPause() {
vContinueClick = 0;
$("table input").attr("disabled", "true");
clearInterval(vTimerID);
}

function fnStop() {
fnPause();
fnShowResult();
}

function fnResetTime() {
$("#hour").text("00");
$("#minute").text("00");
$("#second").text("00");

}

function fnUpdateTime() {
//小龍女短時間內搞定,parseInt()的參數非常重要
var vSecond = parseInt($("#second").text(), 10);
var vMinute = parseInt($("#minute").text(), 10);
var vHour = parseInt($("#hour").text(), 10);
//處理秒數
vSecond++;
if (vSecond >= 0 && vSecond < 10) {
$("#second").text("0" + vSecond);
} else if (vSecond >= 10 && vSecond <= 60) {
$("#second").text(vSecond);
} else {
$("#second").text("00");
//大於60秒,就需處理分鐘
vMinute++;
if (vMinute >= 0 && vMinute <= 9) {
$("#minute").text("0" + vMinute);
} else if (vMinute >= 10 && vMinute <= 60) {
$("#minute").text(vMinute);
} else {
$("#minute").text("00");
//處理小時
vHour++;
if (vHour >= 0 && vHour <= 9) {
$("#hour").text("0" + vHour);
}
else {
$("#hour").text(vHour);
}
}
}
}
</script>
</head>
<body>
<div>
<p>
<span>計時:</span> <span id="hour">00</span>:<span id="minute">00</span>:<span id="second">00</span>
<br />
<input type="button" name="" value="開始" onclick="fnBegin()" />
<input type="button" name="" value="暫停" onclick="fnPause()" />
<input type="button" name="" value="結束" onclick="fnStop()" />
<br />
<input type="button" name="pre" value="生成 2 × 2" onclick="fnInitTable(2)" />
<input type="button" name="" value="生成 2 × 3" onclick="fnInitTable(3)" />
<br />
分數:<span id="score"></span>
</p>
<table>
</table>
</div>
</body>
</html>

實現代碼的過程中,有兩個問題很棘手,一個是開始按鈕連續單擊,計時時間會迅速加快;二是如何判定玩家單擊哪個按鈕。問題得到小龍女的解答,加個標志位,好多問題就迎刃而解的。最大的收獲就是代碼的組織,若想代碼量少,清晰的思路以及解決一個問題的好方法很重要。先實現,後優化,一種值得借鑒的方法!練習中還會有一些bug,希望大家多多指出!
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved