0

Why does the following JavaScript fail, when you click on the link? And is there any way to fix it?

<a onclick="alert('Do you want to delete &lt; &gt; &quot; &#39;?');">Link</a>

I am aware that the escaped characters would be illegal if they were printed literally, but I can't see why it should fail when then are escaped. Leaving out the characters is not an option, as it is user defined.

4

1 回答 1

2

&#39;被认为是单引号,你需要用 a 转义它\,所以\&#39;

如果不转义,javascript 将抛出“未终止的字符串文字”错误。

这有效:

<a onclick="alert('Do you want to delete &lt; &gt; &quot; \&#39;?');">Link</a>
于 2013-05-06T06:59:38.680 回答