gpt4 book ai didi

javascript - 单引号内Javascript双引号的字符串问题

转载 作者:数据小太阳 更新时间:2023-10-29 04:31:32 24 4
gpt4 key购买 nike

这是我的代码:

<script>
function popupTest(title) {
alert(title);
return false;
}
</script>

<a href="" onclick="return popupTest('This is &#039;some&#039; &quot;test&quot; 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\' &quot;test&quot; string')"

使用 unobtrusive JavaScript 可能会更好并使用 JavaScript 绑定(bind)事件处理程序,而不是使用内部事件属性。

关于javascript - 单引号内Javascript双引号的字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5947355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com