为什么▶符号的HTML符号在document.title中不起作用 [英] why does the HTML symbol for ▶ not work in document.title

查看:202
本文介绍了为什么▶符号的HTML符号在document.title中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置 document.title 时尝试使用► ,但它没有渲染播放符号,只是显示了相同的字符。

I tried using ► while setting document.title but it did not render the play symbol, just showed those same characters.

我可以直接粘贴到代码中,这有效。我只是好奇为什么第一种方法不起作用。

I'm able to paste ▶ directly into the code, which works. I'm just curious why the first way wouldn't work.

谢谢!

推荐答案

其他答案是正确的,因为HTML文档的标题是纯文本,不能包含标记。

The other answer is correct in that a HTML document's title is plain text and cannot contain markup.

但是,要明确,您可以使用&#9658; HTML源代码(即< title>&#9658;< / title> )。 解析器会将实体转换为存储在DOM中的字符串中的正确字符。

However, to be clear, you can use &#9658; in the HTML source (ie, <title>&#9658;</title>). The parser will convert the entity in to the proper character in the string that gets stored in the DOM.

您正在运行设置 document.title 时出现问题。即使脚本在HTML文档中内联,HTML解析器也不适用于JavaScript。您的脚本只是将文档的纯文本标题设置为文字ampersand-pound-9658-semicolon,因为该字符序列在脚本中没有特殊含义。

You're running in to an issue when you set document.title. The HTML parser does not apply to JavaScript, even if the script is inline in a HTML document. Your script simply sets the document's plain text title to the literal ampersand-pound-9658-semicolon because that sequence of characters has no special meaning in script.

如果你想要转义脚本源中的字符,使用JavaScript 逃脱序列。 JavaScript中的Unicode转义序列需要Unicode代码点的十六进制表示,并且您的实体使用十进制表示(9568)。转换为十六进制,我们得到25BA:

If you want to escape a character in script source, use a JavaScript escape sequence. Unicode escape sequences in JavaScript require the hex representation of the Unicode code point, and your entity is using the decimal representation (9568). Convert to hex and we get 25BA:

document.title = '\u25ba Playing';

这篇关于为什么▶符号的HTML符号在document.title中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆