gpt4 book ai didi

html - 应该 &'s be escaped in onclick="...”?

转载 作者:行者123 更新时间:2023-11-27 22:32:14 26 4
gpt4 key购买 nike

我还没有找到合适的答案:

onclick="..." 中是否应该转义 &(& 符号)? (或者就此而言,在每个 HTML 属性中?)

我已经尝试在 jsFiddle 和 W3C 的验证器上运行转义和非转义测试,两者都在工作...

http://jsfiddle.net/6LuES

<div onclick="if (1==1 && 2==2) alert('hello there');">Without escape</div>
<div onclick="if (1==1 &amp;&amp; 2==2) alert(&#39;hello there&#39;);">With escape</div>

http://validator.w3.org/#validate_by_input

<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title></title></head><body>

<div onclick="if (1==1 && 2==2) alert('hello there');">Click me</div><br>
<div onclick="if (1==1 &amp;&amp; 2==2) alert(&#39;hello there&#39;);">Click me</div>

</body></html>

谢谢!

最佳答案

如果以下字符未完成某些 HTML 实体,则在您的属性中保留未转义的 & 是有效的。见:

var test = ab&lt;

这将在不将 & 转义为 & 的情况下被破坏(参见 this fiddle )。因此,如果您完全控制您的代码并且您确定其中不会出现任何 HTML 实体,那么您不应该转义 &。它会让你的代码更干净。但是如果有可能某些 HTML 实体可能会出现在这里并破坏所有内容,则您必须转义 &

关于html - 应该 &'s be escaped in onclick="...”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16881041/

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