简体   繁体   English

CSS选择器“如果有这个孩子”

[英]CSS Selector “If it has this child”

I am trying to select this div: 我正在尝试选择此div:

<div class="k-widget k-window">

if it has this child 如果有这个孩子

<div id="window_Monthly">

I've tried: 我试过了:

$("div.k-window:has(div#window_Monthly)")

You should avoid using :has() . 您应该避免使用:has() According to API, 根据API,

Because :has() is a jQuery extension and not part of the CSS specification, queries using :has() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. 因为:has()是jQuery扩展,而不是CSS规范的一部分,所以使用:has()的查询无法利用本机DOM querySelectorAll()方法提供的性能提升。 For better performance in modern browsers, use $("your-pure-css-selector").has(selector/DOMElement) instead. 为了在现代浏览器中获得更好的性能,请改用$(“ your-pure-css-selector”)。has(selector / DOMElement)。


However, there are a few other ways you can go about this: 但是,还有其他几种方法可以解决此问题:

SELECTORS: 选择器:

$("div.k-window div#window_Monthly")
$(".k-window #window_Monthly")

DEMO: http://jsfiddle.net/JHQXd/1/ 演示: http : //jsfiddle.net/JHQXd/1/

IF/LENGTH: IF /长度:

IMO, it would be best to use .find() instead of .children() but both are valid. 国际海事组织,这将是最好使用.find()代替.children()但两者都是有效的。

if($('div.k-window').find('#window_Monthly').length)
     //do something

or 要么

if($('div.k-window').find('#window_Monthly').length > 0)
         //do something

DEMO: http://jsfiddle.net/JHQXd/2/ 演示: http : //jsfiddle.net/JHQXd/2/

Your solution works. 您的解决方案有效。 Please check this jsFiddle : http://jsfiddle.net/JHQXd/ 请检查此jsFiddlehttp : //jsfiddle.net/JHQXd/

$('.k-widget>#window_Monthly').parent()

如果孩子不是一级后代:

$('.k-widget #window_Monthly').parents('.k-widget')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM