Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何中和';在javascript中 var wText=“你在想什么?”; var dCreate=“”+dClear+”;_Javascript_Jquery - Fatal编程技术网

如何中和';在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:谢谢你指出,这个解决方案对我也很有效。。我很抱歉,当我在谷歌搜索时,没有得到那个链接……我的宝贝,没问题!有时你只是不知道该搜索什么:)