我有一个 Html 字符串,我想将它解析为 html,然后删除所有 pre
标签及其子标签。
我试过这个:
HTMLString = "<p>a paragraph</p><p>second Paragraph</p><pre><script> function (){ something(); }</script></pre>";
var $jQueryObject = $($.parseHTML(HTMLString));
alert($jQueryObject.find('pre').length);
但这提醒我 0,这意味着它找不到任何 pre
标记。
谁能告诉我我的代码有什么问题?
HTMLString = "<p>a paragraph</p><p>second Paragraph</p><pre><script> function (){ something(); }</script></pre>";
var $jQueryObject = $($.parseHTML(HTMLString));
alert($jQueryObject.find('pre').length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
最佳答案
HTMLString = "<p>a paragraph</p><p>second Paragraph</p><pre><script> function (){ something(); }</script></pre>";
var $jQueryObject = $("<div/>").html(HTMLString);
console.log("Befor Remove tag: "+ $jQueryObject.find('pre').length);
$jQueryObject.find('pre').remove();
console.log("After Remove tag: "+ $jQueryObject.find('pre').length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
关于javascript - 如何使用 jQuery 删除 html 字符串中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47969084/