本文介绍了在JavaScript onClick事件处理程序中转义双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的简单代码块可以在静态HTML页面中提供,但会导致JavaScript错误。你应该如何逃避onClick处理程序中的嵌入式双引号(即\xyz)?请注意,HTML是通过从数据库中提取数据而动态生成的,其数据是其他HTML代码的片段,可以是单个或双引号。似乎在双引号字符之前添加一个反斜杠并没有办法。
The simple code block below can be served up in a static HTML page but results in a JavaScript error. How should you escape the embedded double quote in the onClick handler (i.e. \"xyz)? Note that the HTML is generated dynamically by pulling data from a database, the data of which is snippets of other HTML code that could have either single or double quotes. It seems that adding a single backslash ahead of the double quote character doesn't do the trick.
<script type="text/javascript">
function parse(a, b, c) {
alert(c);
}
</script>
<a href="#x" onclick="parse('#', false, '<a href=\"xyz'); return false">Test</a>
推荐答案
你尝试过
& quot;
或 \x22
"
or \x22
而不是
\"
?
这篇关于在JavaScript onClick事件处理程序中转义双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!