This one is simple.. I want to remove a child element but return the parent.
In other words, I want to get an element minus the children of a given class..
When I use:
$("#element").clone().find('.elementsToRemove').remove().html();
It doesn't return the #element without the children that have the 'elementsToRemove' class, instead it returns the removed elements..
Question is: how can I return the parent?
Use It is because you are using a filtered subset of elements, use .end()
var html = $("#element").clone().find('.elementsToRemove').remove().end().html(); $('#log').text(html)
#log { border: 1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="log"></div> <div id="element"> <div> <p>something</p> <div class="elementsToRemove">elementsToRemove</div> <div class="elementsToRemove">elementsToRemove</div> </div> <div> <p>something</p> <div class="elementsToRemove">elementsToRemove</div> <div class="elementsToRemove">elementsToRemove</div> </div> </div>
Note: This will return the inner html of #element
使用addBack方法。
$("#element").clone().find('.elementsToRemove').remove().addBack().html();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.