我有一个字符串
var c = '<div class="main">
<div class="child1">text 1</div>
<div class="child1">text 2</div>
<div class="child2">text 2</div>
</div>';
Run Code Online (Sandbox Code Playgroud)
现在我只想child2从这个字符串中删除..
我试过这个
var $c = $(c).not('child2');
Run Code Online (Sandbox Code Playgroud)
但它不起作用任何机构都有解决方案.请告诉我
谢谢
试试这个:
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
Run Code Online (Sandbox Code Playgroud)
不幸
$(c).find('.child2').remove()不会在源头删除它,因为它不在DOM中,只是一个字符串.因此,您需要将对象转换为临时变量,然后将其从中移除并从中取回html字符串.