Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单引号内Javascript双引号的字符串问题_Javascript_String - Fatal编程技术网

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

单引号内Javascript双引号的字符串问题,javascript,string,Javascript,String,这是我的代码: <script> function popupTest(title) { alert(title); return false; } </script> <a href="" onclick="return popupTest('This is &#039;some&#039; &quot;test&quot; string')"><span>Reco

这是我的代码:

<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>
这就像是在解码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\' &quot;test&quot; string')"

您最好使用JavaScript来绑定事件处理程序,而不是使用内部事件属性。

虽然这里的答案是正确的,但正确的解决方案是给元素一个ID,然后使用JavaScript将事件处理程序附加到元素上,而不是内联
onclick
属性。
return popupTest('This is \'some\' "test" string')
onclick="return popupTest('This is \'some\' &quot;test&quot; string')"