为什么我不能使用 js 在 img 标题属性中设置 ascii 引用?
我在图像标题中使用 ascii 字符引用 (®
) 时遇到问题。当您通过 html 主体设置它时,它工作正常,但是当尝试通过 javascript 执行相同的操作时,它不起作用。
检查 sscce:
<style type="text/css">body {background-color:black;}</style>
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
<p>this image has the correct ascii character title:<br /><img src="http://www.prototypejs.org/images/logo-home.gif" id="img1" title="®" /></p>
<p>but why can't I set the same value via javascript?<br /><img src="http://www.prototypejs.org/images/logo-home.gif" id="img2" /></p>
<script type="text/javascript">
$("img2").title = "®";
</script>
谢谢。
I'm having trouble using an ascii character reference (®
) in an image title. It works fine when you set it via the html body, but when trying to do the same thing via javascript does not work.
check the sscce:
<style type="text/css">body {background-color:black;}</style>
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
<p>this image has the correct ascii character title:<br /><img src="http://www.prototypejs.org/images/logo-home.gif" id="img1" title="®" /></p>
<p>but why can't I set the same value via javascript?<br /><img src="http://www.prototypejs.org/images/logo-home.gif" id="img2" /></p>
<script type="text/javascript">
$("img2").title = "®";
</script>
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$("img2").title = "®" 应该可以工作,如果不能使用
$("img2").title ='\u00AE'。
html 实体不会翻译为纯文本。
$("img2").title = "®" should work, if not use
$("img2").title ='\u00AE'.
The html entities are not translated for pure text.
http:// /paulschreiber.com/blog/2008/09/20/javascript-how-to-unescape-html-entities/
但它的基本要点是你设置节点的文本而不是html,所以没有html 实体。
但是,您可以设置隐藏对象的innerHTML 并读取其文本值。或者假设您的源编码允许直接输入 reg 符号。
There are a wealth of answers at http://paulschreiber.com/blog/2008/09/20/javascript-how-to-unescape-html-entities/
But basic gist of it is your setting the text of the node not the html, so there are no html entities.
You can however set the innerHTML of a hidden object and read the text value of that. Or assuming your source encoding allows it just enter the reg symbol directly.