Selecting an element which has another element as direct child
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need to select all div s 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