2013-06-02 289 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 */