![](/img/trans.png)
[英]check class using childNodes and get attribute by javascript
[英]Check if any childnodes exist using jquery / javascript
我有一個帶有div,p和span標簽的DOM結構。 我想用子節點計算'p'標簽,沒有任何子節點。 我在這個論壇上閱讀了一個解決方案,但它對我不起作用: 如何在Javascript中檢查元素是否有子節點? 。 小提琴演示
$('#test').blur(function(){
var test= $('.check p').filter(function (){
if ($(this).childNodes.length > 0)
return this
});
alert(test.lenght)
})
它應該是
$('#test').blur(function(){
var test= $('.check p').filter(function (){
return this.childNodes.length > 0; // as HMR pointed out in the comments if you are looking for child elements then $(this).children().length will do
})
alert(test.length)
})
演示: 小提琴
這里: http : //jsfiddle.net/QN3aM/9/
$('#test').blur(function () {
var test = $('.check p').filter(function () {
return ($(this).children().length)
});
alert(test.length);
})
你只需要在過濾器中返回true
,0是一個假值,其他任何東西都是真的。 你也拼錯了長度。
childNodes是元素的屬性。 當你將元素轉換為jquery對象時,你必須使用jquery方法children()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.