简体   繁体   中英

How can I remove an element that is NOT on the DOM using jQuery?

I want to use jQuery to manipulate a cloned element that is not on the DOM, to perform actions like .remove() on it. Say I have the following code:

var div= $('<div> <div id="div1"></div> </div>');
div.remove('#div1');
console.log(div.html());

The result on the console will still show that the element was not removed. string manipulation is not desirable, I'm looking for something analogue to $().remove()

The div variable will contain a reference to the outer div . You need to use find() to get the inner div by its id :

var $div = $('<div><div id="div1"></div></div>');
$div.find('#div1').remove();

使用jQuery()函数的context参数:

$('div', div).remove('#div1');

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM