我有一个字符串
var c = '<div class="main">
<div class="child1">text 1</div>
<div class="child1">text 2</div>
<div class="child2">text 2</div>
</div>';
现在我只想从此字符串中删除 child2
..
我试过了
var $c = $(c).not('child2');
但它不起作用 任何机构对此都有解决方案。请告诉我
谢谢
最佳答案
试试这个:
var c ='<div class="main"><div class="child1"> text 1</div><div class="child1"> text 2</div><div class="child2"> text 2</div></div>';
var $c = $(c); //Get the JQ object to a var
$c.find('.child2').remove(); //Remove it from there
c = $c.html(); //Get back the html
不幸的是
$(c).find('.child2').remove()
不会从源代码中删除它,因为它不在 DOM 中,只是一个字符串。因此,您需要将对象获取到临时变量,然后从中删除它并从中取回 html 字符串。
Demo
关于jquery - 如何使用jquery从字符串中删除具有特定类或id的div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17057850/