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?
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?
Use the :has
selector:
$("div:has(> object)").css("text-align", "center");
Here's an example I wrote up:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
div { padding: 15px; border: 1px solid black; }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("div:has(> h3)").css("background", "yellow");
});
</script>
</head>
<body>
<div>
<h2>outer</h2>
<div>
<h3>inner</h3>
</div>
</div>
</body>
</html>
You can simplify your way:
$('div:has(> object)').css('text-align', 'center');
..fredrik