[英]Update div when click button
Can someone tell me what is wrong with this piece of code. 有人可以告诉我这段代码有什么问题吗? i am trying to update the div with the class display whenever the a tag is click 每当单击标签时,我试图用类显示更新div
var current = 0;
function nextPage() {
current++;
return current;
}
$(document).ready(function (e) {
$("a").click(function () {
$(".display").text(nextPage());
return false;
});
});
In your code line 在您的代码行中
$(".display").text(nextPage());
$(".display")
returns a set of elements. $(".display")
返回一组元素。 Your actual target element might not be the first therein. 您的实际目标元素可能不是其中的第一个。 Use $.each to iterate over every one or use the id-selector to specify a single DOM-element. 使用$ .each遍历每个对象,或使用id选择器指定单个DOM元素。
.each()
example: ( Demo ) .each()
示例:( 演示 )
$(document).ready(function (e) {
$("a").click(function () {
$(".display").each(function (index, element) {
$(element).text(nextPage());
});
return false;
});
});
id-selector example: ( Demo ) id选择器示例: ( Demo )
$(document).ready(function (e) {
$("a").click(function () {
$("#display2").text(nextPage());
return false;
});
});
To ensure that all dynamically inserted elements will have an event use .on
. 为确保所有动态插入的元素都具有事件,请使用.on
。
This example on jsFiddle jsFiddle上的这个例子
$(document).on("click", "a", function () {
$(".display").text(nextPage());
return false;
});
Note: <!-- comment -->
only works on HTML, for Javascript use // comment
or /* comment */
注意: <!-- comment -->
仅在HTML上有效,对于Javascript使用// comment
或/* comment */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.