2013-06-02 69 views
0

有人能告訴我這段代碼有什麼問題。我試圖每當一個標籤點擊當點擊按鈕時更新div

var current = 0; 

function nextPage() { 
    current++; 
    return current; 
} 

$(document).ready(function (e) { 
    $("a").click(function() { 
     $(".display").text(nextPage()); 
     return false; 
    }); 
}); 
+0

什麼錯誤的事情,現在是怎麼回事?你收到錯誤信息或看到一些相關的行爲? –

+0

刪除,他們是無效的js意見 – Chris

+0

我看到代碼中沒有錯:http://jsfiddle.net/ZtSMm/ – karthikr

回答

0

在你的代碼行

$(".display").text(nextPage()); 

$(".display")返回一組元素的更新與等級顯示股利。您的實際目標元素可能不是其中的第一個。使用$ .each遍歷每個元素或使用id-selector指定單個DOM元素。


.each()例如:Demo

$(document).ready(function (e) { 
    $("a").click(function() { 
     $(".display").each(function (index, element) { 
      $(element).text(nextPage()); 
     }); 
     return false; 
    }); 
}); 

ID-選擇例如:Demo

$(document).ready(function (e) { 
    $("a").click(function() { 
     $("#display2").text(nextPage()); 
     return false; 
    }); 
}); 
0

爲了確保所有達因amically插入的元素將有一個事件使用.on

This example on jsFiddle

$(document).on("click", "a", function() { 
    $(".display").text(nextPage()); 
    return false; 
}); 

注:<!-- comment -->僅適用於HTML,Javascript的使用// comment/* comment */