我是Jquery的新手,在一本書中看到了這段代碼。 我想了解如何執行hideCode()。Jquery函數調用序列
這是我evets序列的認識多數民衆贊成將要發生:
- 文件被加載和準備執行的jQuery功能。
- 當guess_box被點擊時運行checkForCode()函數。
- hideCode()函數運行。
這是正確的嗎?
$(document).ready(function() {
$(".guess_box").click(checkForCode);
function getRandom(num) {
var my_num = Math.floor(Math.random() * num);
return my_num;
}
var hideCode = function() {
var numRand = getRandom(4);
$(".guess_box").each(function(index, value) {
if(numRand == index){
$(this).append("<span id='has_discount'></span>");
return false;
}
});
}
hideCode();
function checkForCode() {
var discount;
if($.contains(this, document.getElementById("has_discount"))) {
var my_num = getRandom(5);
discount = "<p>Your Discount is " + my_num + "%</p>";
} else {
discount = "<p>Sorry, no discount this time!</p>" ;
}
$(this).append(discount);
$(".guess_box").each(function() {
$(this).unbind('click');
});
恕我直言,在行中添加警報/console.debug並查看它們執行的順序。這將幫助你理解執行的順序。 – Shyju 2012-04-26 21:14:29
您的代碼很難用1空格縮進來閱讀。我建議你使用4個空格。 – ThiefMaster 2012-04-26 21:17:18
hideCode在您點擊之前被調用。 – jantimon 2012-04-26 21:50:30