Selecting an element which has another element as direct child

匿名 (未验证) 提交于 2019-12-03 01:13:01

问题:

I need to select all divs which contain object as their direct child. :has just checks for descendants of any kind, so now I'm using:

$('div > object').parent().css('text-align', 'center'); 

is there a more direct way?

回答1:

You can simplify your code to:

$('div:has(> object)').css('text-align', 'center'); 


回答2:

Use the :has selector:

$("div:has(> object)").css("text-align", "center"); 

Here's an example I wrote up:

$(function() {   $("div:has(> h3)").css("background", "yellow"); });
div { padding: 15px; border: 1px solid black; }

outer

inner



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!