Home >php教程 >PHP开发 >A brief discussion on the processing of special characters contained in jquery's html method

A brief discussion on the processing of special characters contained in jquery's html method

高洛峰
高洛峰Original
2016-12-03 14:27:391769browse

When using jquery’s html() method, sometimes the html code added inside contains some special characters and needs to be escaped.

The following example:

inst_html = "<a style=color:white&#39; onmouseover = &#39;";
inst_html += "javascript:showme(&#39;"+inst.instId+"_"+valId+"&#39;);";
inst_html += "&#39; ";
$("#inst_div_"+valId).html(inst_html);

If you write it directly like this, there will be no problem under chrome and FF browsers, but an error will be reported under IE8.

The solution is to change 'escape' in javascript to ', so that no error will be reported.

The above example is changed to:

inst_html = "<a style=color:white&#39; onmouseover = &#39;";
inst_html += "javascript:showme(&#39;"+inst.instId+"_"+valId+"&#39;);";
inst_html += "&#39; ";
$("#inst_div_"+valId).html(inst_html);

Some characters are escaped as follows:

•& symbol: "&", "&"
•Double quotes: """, """
•Less-than symbol: "<", "

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn