这是我的代码:
<script>
function popupTest(title) {
alert(title);
return false;
}
</script>
<a href="" onclick="return popupTest('This is 'some' "test" string')"><span>Recommend</span></a>
使用 Firefox 4 我得到错误:
Error: missing ) after argument list
Source Code:
return popupTest('This is 'some' "test" string')
它就像是在解码 HTML 实体,但我不知道为什么。
也试过...
<a href="" onclick="return popupTest('This is \'some\' \"test\" string')"><span>Recommend</span></a>
给出错误:
Error: unterminated string literal
Source Code:
return popupTest('This is \'some\' \
最佳答案
'
是'
的HTML。因此对于第一个示例,解析 HTML 并传递 JavaScript 引擎:
return popupTest('This is 'some' "test" string')
…第二个'
终止字符串。
另一方面:
onclick="return popupTest('This is \'some\' \"test\" string')"
解析为:
An onclick attribute with the value
return popupTest('This is \'some\' \
followed by some invalid data.
您需要先处理 JavaScript:
return popupTest('This is \'some\' "test" string')
和然后将其转义为 HTML:
onclick="return popupTest('This is \'some\' "test" string')"
使用 unobtrusive JavaScript 可能会更好并使用 JavaScript 绑定(bind)事件处理程序,而不是使用内部事件属性。
关于javascript - 单引号内Javascript双引号的字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5947355/