如何中和';在javascript中 var wText=“你在想什么?”; var dCreate=“”+dClear+”;
当我在如何中和';在javascript中 var wText=“你在想什么?”; var dCreate=“”+dClear+”;,javascript,jquery,Javascript,Jquery,当我在文本丢失后追加html时,请帮助我如何中和字符您需要为html编码字符串。例如,单引号应为': jQuery解决方案: var wText = "What's on your mind ?"; var dCreate = "<div class='createpost-sec'><textarea class='clicktxt' rows='1' cols='1' placeholder='"+wText+"'/>"+dClear+"</div>
文本丢失后追加html
时,请帮助我如何中和
字符您需要为html编码字符串。例如,单引号应为'代码>:
jQuery解决方案:
var wText = "What's on your mind ?";
var dCreate = "<div class='createpost-sec'><textarea class='clicktxt' rows='1' cols='1' placeholder='"+wText+"'/>"+dClear+"</div></div></div>";
function htmlEncode(value){
//create a in-memory div, set it's inner text(which jQuery automatically encodes)
//then grab the encoded contents back out. The div never exists on the page.
return $('<div/>').text(value).html();
}
function htmlDecode(value){
return $('<div/>').html(value).text();
}
函数htmlEncode(值){
//创建内存中的div,设置其内部文本(jQuery自动编码)
//然后把编码的内容拿回来。div在页面上永远不存在。
返回$('').text(value.html();
}
函数htmlDecode(值){
返回$('').html(value.text();
}
手动解决方案:
var wText = "What's on your mind ?";
var dCreate = "<div class='createpost-sec'><textarea class='clicktxt' rows='1' cols='1' placeholder='"+wText+"'/>"+dClear+"</div></div></div>";
function htmlEncode(value){
//create a in-memory div, set it's inner text(which jQuery automatically encodes)
//then grab the encoded contents back out. The div never exists on the page.
return $('<div/>').text(value).html();
}
function htmlDecode(value){
return $('<div/>').html(value).text();
}
函数htmlEscape(str){
返回字符串(str)
.更换(/&/g,“&;”)
.替换(/“/g,”))
.替换(/'/g'';'))
.替换(//g',);
}
函数htmlUnescape(值){
返回字符串(值)
.替换(/“/g,”))
.替换(/';/g,“”)
.replace(//g',)
.替换(/&;/g,“&”);
}
@FelixKling:谢谢你指出,这个解决方案对我也很有效。。我很抱歉,当我在谷歌搜索时,没有得到那个链接……我的宝贝,没问题!有时你只是不知道该搜索什么:)