2010-08-23 92 views
1

我知道如何使用jQuery遍歷XML元素的兒童:jQuery檢查xml元素是否有孩子?

$(this).children('tag_name').each(function() { 

}); 

有沒有一種方便的方式來簡單地測試如果一個元素有孩子嗎?事情是這樣的不正確的代碼:

$(this).hasChildren('tag_name') //returns true or false 

回答

6

使用此方法:

var hasChildren = $(this).children('tag_name').length > 0; 
1

$(this).find('tag_name').length > 0所有後代之間尋找 $(this).children('tag_name').length > 0爲inmediate兒童

0

你可以這樣做 -

​​
0

有沒有簡單的方法來測試一個元素是否有孩子?

如果您沒有設置特定的元素名稱,這是最直接的方法。

this.childElementCount > 0