[英]how determine if object has child of certain element?
I have a very simple function, which I want to work only if selected object has child of certain type - in this case if it has ul nested. 我有一个非常简单的函数,只有在选定对象具有某种类型的子对象的情况下,我才想使用它-在这种情况下,如果它具有ul嵌套。
I have tried with this: 我已经尝试过:
var onMouseOver = function() {
if (this.getElementsByTagName('ul') > 0);{
console.log('entered');
}
}
and this: 和这个:
var onMouseOver = function() {
if (this.querySelector('ul') != null);{
console.log('enter');
}
}
but it doesn't help - function still launches, even when it returnes 'null'. 但这无济于事-即使返回“ null”,功能仍会启动。
You should be looking at the length property of those selectors. 您应该查看这些选择器的length属性。
var onMouseOver = function() { if (this.querySelectorAll('ul').length > 0);{ console.log('enter'); } }
or 要么
var onMouseOver = function() { if (this.getElementsByTagName('ul').length > 0);{ console.log('entered'); } }
try this one :) 试试这个:)
function ok(obj) { var l = obj.children.length; for (var i = 0; i <= l; i++) { if (obj.children[i].tagName == "UL") { var c = obj.children[i].children[0]; alert("child id is :-" + c.tagName); alert("child id is :-" + c.id); alert("child value is :-" + c.innerHTML); break; } } }
#child { display: none; }
<!DOCTYPE html> <html> <body> <div id="Parent" onmouseover="ok(this)">Find Child ? mouse over me <p id="child">I'm Child</p> <ul> <li id="list">hello</li> </ul> </div> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.