我正在尝试使用转义字符。
<img src="x" onerror=alert('hello'); />
<img src="x" onerror="alert(\"hello\")" />
第二个在开发控制台中产生非法字符
。为什么?为什么我不能使用转义字符?
最佳答案
因为 \
不是 HTML 中的转义字符。
<img src="x" onerror="alert("hello")" />
或者,根据@mplungjan 的评论:
<img src="x" onerror="alert('hello')" />
或者,更好的是:
<img src="x" id="derp" />
<script type="text/javascript">
document.getElementById('derp').addEventListener("error",function() {
alert('hello');
},false);
</script>
关于javascript - <img src ="x"onerror ="alert(\"hello\")"/>) 转义引号是非法的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24515021/