简体   繁体   中英

jQuery is Commenting out my PHP Code Snippets

I'm building a code snippet system and am using jQuery to display the editor. I noticed jQuery likes to comment out any inline PHP code. Is there a way to prevent this from happening:

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

jQuery code:

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

This is what I would like to see in the textarea:

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

But instead jQuery modifies it to look like this:

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

I understand this is probably a security measure, but is there a way to stop this from happening?

I know I can use HTML entities to get around this, but due to the nature of this tool it would interfere with code snippets being posted that intentionally include html entities.

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

Additional Node : Some answers suggest I use JavaScript to handle this without jQuery, but I need jQuery. My string contains a DOM tree that I'm using jQuery to parse.

尝试这个:

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

This does the trick.

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

It is not valid to use other elements inside of a textarea-element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM