[英]Get all child elements by its parent
這是div結構。
<div id="parentDiv">
<input type="text" id="tf1">
<input type="text" id="tf2">
<input type="text" id="tf3">
<button id="bt3">Text</button>
</div>
我通過指定各個ID使用以下代碼。
編輯
有一個$(document).click
設置,如果用戶當前在任何這些文本字段中鍵入任何內容,我都避免觸發函數。 一切正常。 我的問題是不要添加單獨的元素,因為目標是否可以在父對象中包含所有元素?
$(document).click(function(e) {
if ($(e.target).closest("#tf1, #tf2, #tf3, #btn3").length) { //Can these individual elements be replaced by using parent div ID?
return;
}
if (typeof commonFunc == "function"){
commonFunc();
}
});
通過這樣做,我不需要跟蹤添加的新元素。
解決方案是
if($(e.target).closest("#parentDiv").children().length)
$(e.target).children() // Will return all the children of whatever e.target is
編輯
$(document).click(function(e) {
if ($(e.target).children().length==0) {
// No children element.
return;
}
else{
// There are some child elements.
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.