jQuery的每个选择器不起作用 [英] jquery each selector doesnt work
本文介绍了jQuery的每个选择器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个计数器,每个计数器都对不同的数字进行计数,但是当我在课堂上选择所有要素时,它就不起作用了...
I created a counter that count different numbers each, but when I choose all the elemnts in class it doesnt work...
$(document).ready(function() {
function change() {
/*
$(".test").each(function(i,domeElement){
var theNum = parseInt(this.html())+1;
this.html(theNum);
});
*/
//this works... the other one doesnt why?!?!
var theNum = parseInt($(".test").html()) + 1;
$(".test").html(theNum);
}
setInterval(change, 1000);
});
推荐答案
您应该使用
$(this).html(theNum);
不是
this.html(theNum);
因为html()
是jQuery函数,而this
(在.each()
内部)是domElement,所以必须将其包装到jQuery对象中
Because html()
is a jQuery function and this
(inside .each()
) is a domElement and so you must wrap it into a jQuery object
在这里拨弄: http://jsfiddle.net/nicolapeluchetti/DTyY7/2/
这篇关于jQuery的每个选择器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文