Is there a built-in jQuery function for encoding a string as HTML?
I'm 开发者_高级运维trying to take the text a user types into a text box and then put that text into a different area of the page. My plan was to take the .val()
from the text box and supply that to the .html()
of the <div>
element. Perhaps there's a good jQuery plugin to help with this (if it's not built-in) or a better way overall to accomplish this goal.
For example, if the user puts <Victory!>
in the text box, I'd want the other part of the page to actually show the text <Victory!>
instead of nothing being visible.
Instead of .html()
use .text()
for this. This will encode the output when putting it in the destination element. Here's a quick demo showing the differences, try something like "<script>"
: http://jsfiddle.net/6WG47/
If the .val() contains HTML markup (i.e. text), that should automatically render if you pass it directly to .html(). Are you experiencing something otherwise? Or are you wanting to strip the html encoding?
精彩评论