0

是否可以在不实际编辑 postbody 变量内容的情况下替换'postbody 变量中的'

var postbody = '<div class="postbody">this is tom's and bill's post</div>';
var postid = $('#post-content');
postid.append(postbody);​

http://jsfiddle.net/3nVrZ/

4

4 回答 4

3

创建一个元素并设置其文本,然后您不必对其进行编码:

var post = $('<div/>', { 'class': 'postbody' });
post.text("this is tom's and bill's post");
$('#post-content').append(post);​
于 2012-05-22T10:15:11.397 回答
2

你可以使用

var postbody = '<div class="postbody">this is tom\'s and bill\'s post</div>';

或者

var postbody = "<div class=\"postbody\">this is tom's and bill's post</div>";
于 2012-05-22T10:09:28.963 回答
0

如果您真的想替换字符串中的某些内容,只需使用 replace():

var postbody = "<div class=\"postbody\">this is tom's and bill's post</div>";
postbody = postbody.replace(/\'/g, "&#39;");
于 2012-05-22T10:17:41.320 回答
0

尝试使用这个

var postbody = "<div class='postbody'>this is tom's and bill's post</div>";
var postid = $('#post-content');
postid.append(postbody);​

http://jsfiddle.net/ipsjolly/3nVrZ/1/

于 2012-05-22T10:19:14.453 回答