开发者

Replace Single Quotes in Javascript or JQuery

开发者 https://www.devze.com 2022-12-19 10:43 出处:网络
i have an Html String in which i have som开发者_开发百科e elements having single quotes.When i put this inside a $(\'varHtml\'); Since the varHtml already contains some single quotes it qives an error

i have an Html String in which i have som开发者_开发百科e elements having single quotes.When i put this inside a $('varHtml'); Since the varHtml already contains some single quotes it qives an error, Can Somebody help me how to Escape the single quotes in the varHtml

Thanks in Advance

Thomson


If you have a HTML string in a variable, then you don't need to put it in quotes:

var varHtml = "<div id='foo'></div>";
$(varHtml);


javascript lacks something like an htmlencode to run client side. So you will have to use one of the script libraries. You can try this jQuery solution: http://www.edentity.ca/WhoWeAre/Blog/Easy-Client-Side-html-EncodeDecode-using-jQuery.aspx Or you could simply use a javascript string replace function like the one explained here: http://www.w3schools.com/jsref/jsref_replace.asp. Replace ' with &#39; or the HTML code you prefer. Reference: http://www.degraeve.com/reference/specialcharacters.php

0

精彩评论

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