[英]Append increasing number on click
I'm trying to append an increasing number to elements on click. 我试图将越来越多的点击添加到元素上。 I can't seem to make it work. 我似乎无法使其工作。
My code: 我的代码:
$('#on').click(function() {
$("b").click(function(e) {
var numCount = ($("[span class='num'>").length + 1);
var element = $("<span class='num'>" + numCount + "'>" + numCount + "</span>");
$(this).append(element);
});
});
I think It's a simple syntax error in my code, but I'm learning here so I could be completely wrong. 我认为这是代码中的一个简单语法错误,但是我在这里学习,所以我可能会完全错误。 It's important for the class to be added too. 对于班级也很重要。
Change 更改
var numCount = ($("[span class='num'>").length + 1);
to 至
var numCount = ($(".num").length + 1);
You need to find .num
elements, not create new ones. 您需要查找.num
元素,而不是创建新元素。
In addition, your element
line doesn't create valid HTML either. 此外,您的element
行也不会创建有效的HTML。 Try the following: 请尝试以下操作:
var element = $("<span class='num'>" + numCount + "</span>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.