jQuery正在评论我的PHP代码段


jQuery is Commenting out my PHP Code Snippets

我正在构建一个代码片段系统,并使用jQuery来显示编辑器。我注意到jQuery喜欢注释掉任何内联PHP代码。有没有办法防止这种情况发生:

<textarea id='beep'></textarea>

jQuery代码:

var code = "<div>Why <?php echo 'hello'; ?> there!</div>";
var parsed = $(code).html();
$('#beep').val(parsed);

这是我想在文本区域看到的:

Why <?php echo 'hello'; ?> there!

但是jQuery将其修改为如下所示:

Why <!--?php echo 'hello'; ?--> there!

我知道这可能是一种安全措施,但有办法阻止这种情况发生吗?

我知道我可以使用HTML实体来解决这个问题,但由于这个工具的性质,它会干扰发布的有意包含HTML实体的代码片段。

JSFiddle:http://jsfiddle.net/YB4fD/1/

附加节点:有些答案建议我在没有jQuery的情况下使用JavaScript来处理此问题,但我需要jQuery。我的字符串包含一个DOM树,我正在使用jQuery来解析它。

试试这个:

var code = "<div>Why &lt;?php echo 'hello'; ?&gt; there!</div>";

这就成功了。

var code = "Why <?php echo 'hello'; ?> there!";
document.getElementById('beep').appendChild(document.createTextNode(code));

在textarea元素内部使用其他元素是无效的。