假设我在html中有这个:
<code><?php die(); ?></code>
现在,我希望得到与<?php die(); ?>
相同的结果,但它与html评论相符:
<!--?php die(); ?-->
$('code').text($('code').html());
那么,现在我如何从文本中删除!--
和--
?
答案 0 :(得分:0)
使用 <pre>
<pre><?php die(); ?><pre>
或者
<code><?php die(); ?><code>
您应该转义>,<,&
<强> Updated Demo 强>
提示:您在问题中发布的代码(4个空格) - 通过检查元素了解Stack Overflow是如何做到的。
答案 1 :(得分:0)
var c = $("code").contents().filter(function() {
return this.nodeType == 8; //comment node
}).get(0).data;
//add '<' and '>', because they are replaced as part of comment by filter()
$("code").text("<" + c + ">"); //gives <?php die(); ?>
更新了Demo