如果我有以下html:
<div id="main">
This <span class="class-two">is</span> a <span class="class-one">string</span> and I want <span class="class-one">to remove</span> spans <span>with the</span> class class-one.
</div>
如何删除所有具有班级class-one
?
上述html的预期输出为:
<div id="main">
This <span class="class-two">is</span> a string and I want to remove spans <span>with a</span> class
</div>
第二级和没有班级避风港的跨度已被删除。
我还需要将新的html字符串保存到变量中,这是我正在努力的部分。
这样的事情:
var html = $('#main').$('.class-one').replaceWith((i, txt) => txt);
答案 0 :(得分:1)
选择#main
,然后找到.class-one
。使用.replaceWith()
with a function替换文本。然后使用.end()
返回.main
并获取.html()
:
const result = $('#main')
.find('.class-one')
.replaceWith((i, txt) => txt)
.end()
.html();
console.log(result);
&#13;
.class-one {
color: red;
}
.class-two {
color: blue;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
This <span class="class-two">is</span> a <span class="class-one">string</span> and I want <span class="class-one">to remove</span> spans <span>with the</span> class class-one.
</div>
&#13;