开发者

Escaping html in Jquery

I have a simple jquery snippet here:

$("#someid").html("Text Here");

The only problem I am having is that I am putting Database data inside the html().

$("#someid").html("<?php echo $row['tablecolumn']; ?>");

It works great! Exce开发者_高级运维pt for one thing.

It does not show when I have 'breaks' in the text from the database.

I'm sure I need to do some sort of escaping of characters. But I don't know quiet what that is. Any ideas out there?


Use nl2br to add HTML line breaks to the physical ones and encode the data with json_encode:

$("#someid").html(<?php echo json_encode(nl2br($row['tablecolumn'])); ?>);


So, your text does not show up with line breaks as you'd expect?

Perhaps you need to convert the line breaks to
tags on the server side before sending the data over. If you're using PHP for instance, you could use the nl2br function for to create HTML line breaks anywhere there is a \n in the text.


Use .text() instead of .html().

From jQuery Attributes/text

Set the text contents of all matched elements. Similar to html(), but escapes HTML (replace "<" and ">" with their HTML entities). Cannot be used on input elements. For input field text use the val attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜