我有一个这样的div
<div id="asdf">
<div>
<p>ASDF IS AWESOME</p>
</div>
<h1>Just another Headline</h1>
<nav>
<ul>
<li class="listitem">Hey</li>
</ul>
</nav>
</div>
并且需要 jQuery
或vanilla JavaScript
函数,将所有内容转换为 this 的方法
<div id="asdf"
<div>
<p>ASDF IS AWESOME</p>
</div>
<h1>Just another Headline</h1>
<nav>
<ul>
<li class="listitem"Hey</li>
</ul>
</nav>
</div>
所以我只是想逃避 <
和>
我试过.replace
和.text()
最佳答案
试试这个
var html = $('#asdf').html();
var escaped=$('<div/>').text(html).html();
console.log(escaped);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="asdf">
<div>
<p>ASDF IS AWESOME</p>
</div>
<h1>Just another Headline</h1>
<nav>
<ul>
<li class="listitem">Hey</li>
</ul>
</nav>
</div>
关于javascript - jQuery 或普通 JavaScript : Replace or escape all < and >,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46518617/