如何在 JavaScript 中正确转义 JavaScript?
这可能是我不能做的事情,但是...
parent.document.getElementById('<?php echo $_GET['song']; ?>')
.innerHTML = '<img src="heart.png" onmouseover="heartOver('');" >';
onmouseover="heartOver('');
部分破坏了我的 JavaScript。有没有办法转义引号,以便我可以做到这一点?
This might be something I can't do but...
parent.document.getElementById('<?php echo $_GET['song']; ?>')
.innerHTML = '<img src="heart.png" onmouseover="heartOver('');" >';
The onmouseover="heartOver('');
portion breaks my JavaScript. Is there a way to escape the quotes so I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这只是正确转义引号的问题。
此外,您还缺少 alt 属性的结束引号。
It's all just a matter of escaping the quotes properly.
Also you're missing and end quote for the alt attribute.
使用反斜杠转义嵌套引号:
\'
另外,从不在不验证或清理用户数据的情况下回显用户数据:
或者
Escape nested quotes with a backslash:
\'
Also, never echo user data without validating or sanitizing it:
OR
您的问题是转义引号。 您可以通过将 ' 或 " 替换为 \' 或 '" 来转义单引号或双引号。
所以你的代码是:
parent.document.getElementById('').innerHTML = '
Your issue is with escaping quotes. You can escape either a single or double quote by replacing ' or " with \' or '".
So your code would be:
parent.document.getElementById('<?php echo $_GET['song']; ?>').innerHTML = '<a href="heart.php?action=unlove&song=<?php echo $song; ?>" target="hiddenframe"><img src="images/heart.png" alt="Love! onmouseover="heartOver(\'\');" width="16" height="16" border="0"></a>';