The method optionally accepts a selector expression of the same type that we can pass to the
$()
function. If the selector is supplied, the elements will be filtered by testing whether they match it.
The result of this call is a red background for the level-2 list. Since we do not supply a selector expression, the parent element is unequivocally included as part of the object. If we had supplied one, the element would be tested for a match before it was included.
Shows the parent of each element as (parent > child). Check the View Source to see the raw html.
<title>parent demo</title>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
$( "*", document.body ).each(function() {
var parentTag = $( this ).parent().get( 0 ).tagName;
$( this ).prepend( document.createTextNode( parentTag + " > " ) );
<title>parent demo</title>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<div class="selected"><p>Hello Again</p></div>
$( "p" ).parent( ".selected" ).css( "background", "yellow" );
Web hosting by Digital Ocean
|
CDN by Fastly
|
Powered by WordPress