单引号内Javascript双引号的字符串问题
这是我的代码:单引号内Javascript双引号的字符串问题,javascript,string,Javascript,String,这是我的代码: <script> function popupTest(title) { alert(title); return false; } </script> <a href="" onclick="return popupTest('This is 'some' "test" string')"><span>Reco
<script>
function popupTest(title) {
alert(title);
return false;
}
</script>
<a href="" onclick="return popupTest('This is 'some' "test" string')"><span>Recommend</span></a>
这就像是在解码HTML实体,但我不知道为什么
我也试过
<a href="" onclick="return popupTest('This is \'some\' \"test\" string')"><span>Recommend</span></a>
'代码>是的HTML,
。因此,对于第一个示例,解析HTML并传递JavaScript引擎:
return popupTest('This is 'some' "test" string')
…第二个“
终止字符串
另一方面:
onclick="return popupTest('This is \'some\' \"test\" string')"
解析为:
值为的onclick属性返回poputest('This'some\\\\
,后跟一些无效数据)
您需要首先处理JavaScript:
return popupTest('This is \'some\' "test" string')
和然后将其转义为HTML:
onclick="return popupTest('This is \'some\' "test" string')"
您最好使用JavaScript来绑定事件处理程序,而不是使用内部事件属性。虽然这里的答案是正确的,但正确的解决方案是给元素一个ID,然后使用JavaScript将事件处理程序附加到元素上,而不是内联onclick
属性。
return popupTest('This is \'some\' "test" string')
onclick="return popupTest('This is \'some\' "test" string')"