<div class="post-content">
<p>xxxxx</p>
</div>
<div class="post-content">
<p>Test1</p>
<p>Test2</p>
<p>Test3</p>
</div>$(document).ready(function () {
$('.post-content').each(function () {
//alert($(this).children.length);
alert($(this).length);
console.log($(this).html());
});
});http://jsfiddle.net/7cXKr/
长度应该是post内容的p子类的数量。为什么输出是不正确的?
在这种情况下,1和3
发布于 2013-12-05 02:10:44
children是一种方法,您必须将其称为:
alert($(this).children().length);
// call the method ^^更新的演示
$(this).length总是返回1,因为this是DOM元素,$(this)用一个元素创建一个选择。
发布于 2013-12-05 02:11:09
因为上下文(这个)是一个jQuery对象,而不是
$(this).length您可以使用
$(this).children().lengthhttps://stackoverflow.com/questions/20390068
复制相似问题