简体   繁体   English

无法使用 Javascript 将 HTML 按钮元素的标题或值设置为 C 度符号

[英]Unable to set the caption or value of a HTML button element to the degree C symbol using Javascript

An ESP8266 is used to dynamically generate a webpage that includes buttons. ESP8266 用于动态生成包含按钮的网页。 The Javascript is embedded in c. Javascript 嵌入在 c 中。 The Label (aka caption aka value) of the button toggles depending on the mode.按钮的标签(又名标题又名值)根据模式切换。

The code that sets the button caption looks like this设置按钮标题的代码如下所示

document.getElementByID('idUnits').value='°C'

When using chrome's development tools and viewing the generated html it looks like当使用 chrome 的开发工具并查看生成的 html 时,它看起来像

<input type="button" value="&#176;C" id="idUnits" onclick="dendbuttons(this.id);">

If I hover over the value and right click "edit attribute" it shows &#176;C however if I hover over and "Edit as HTML" it gives me this to edit which includes an unwanted amp;如果我将鼠标悬停在该值上并右键单击“编辑属性”,它会显示&#176;C但是,如果我将鼠标悬停在该值上并“编辑为 HTML”,它会给我这个编辑,其中包括一个不需要的amp;

<input type="button" value="&amp;#176;C" id="idUnits" onclick="dendbuttons(this.id);">

if I remove the amp;如果我移除放大器; while editing (in Chrome's development tool) the button html displays the correct symbol.在编辑时(在 Chrome 的开发工具中)按钮 html 显示正确的符号。

Why does amp;为什么 amp; get added to the value of the button.被添加到按钮的值中。 How do I encourage it not to do so.我如何鼓励它不要这样做。 I am using a similar technique to dynamically update other html elements with the degree symbol without issue.我正在使用类似的技术来动态更新带有度数符号的其他 html 元素,而不会出现问题。 Only when attempting to do this with the "Button" element do I have issues.只有在尝试使用“按钮”元素执行此操作时,我才会遇到问题。

It is because the value property does not parse your HTML.这是因为value属性不会解析您的 HTML。 So the htmlEntity (degrees symbol) string will not yet be seen as one.所以 htmlEntity(度数符号)字符串还不会被视为一个。 Although when using "Edit as HTML" you'll suddenly parse everything that you've selected as HTML, including your htmlEntity string.尽管在使用“Edit as HTML”时,您会突然将您选择的所有内容解析为 HTML,包括您的 htmlEntity 字符串。

I would recommend you using a button element instead.我建议您改用按钮元素。

<button type="button" id="idUnits" onclick="dendbuttons(this.id);"></button>

And add the htmlEntity with the .innerHTML setter which will parse your string as HTML and thus decodes the symbol and renders it.并添加带有.innerHTML setter 的 htmlEntity,它会将您的字符串解析为 HTML,从而对符号进行解码并呈现它。

document.getElementByID('idUnits').innerHTML = '&#176;C';

You can't use HTML entities inside JS strings as they won't be parsed, but you can use the escape notation to represent them with their Unicode code.你不能在 JS 字符串中使用 HTML 实体,因为它们不会被解析,但你可以使用转义符号用它们的 Unicode 代码来表示它们。

So, how do you get the Unicode code of ° ...?那么,如何得到° ... 的 Unicode 代码呢? 🤔 🤔

One option is to just look it up .一种选择是 查找它

The other one, if you can type or copy-paste the original character from somewhere else, is to get it using String.prototype.charCodeAt() , which returns the UTF-16 code unit (decimal) at the given index, and Number.prototype.toString() , using its radix parameter to convert that decimal to hexadecimal:另一种,如果您可以从其他地方键入或复制粘贴原始字符,则使用String.prototype.charCodeAt()获取它,它返回给定索引处的 UTF-16 代码单元(十进制)和Number.prototype.toString() ,使用其radix参数将该十进制转换为十六进制:

'°'.charCodeAt(0); // 176
'°'.charCodeAt(0).toString(16); // "b0"

So, using the \\xXX notation, your code should be:因此,使用\\xXX符号,您的代码应该是:

 document.getElementById('idUnits').value='\\xb0C';
 <input type="button" id="idUnits">

Lastly, rather than using two characters ° + C = °C , you might want to use this other specific character: .最后,与其使用两个字符° + C = °C ,不如使用另一个特定字符:

Same thing here, but using the \\uXXXX notation instead:这里也一样,但使用\\uXXXX符号代替:

'℃'.charCodeAt(0).toString(16); // "2103"

So then the code would look like this:那么代码将如下所示:

 // Note the escape notation here is different, as // explained in MDN's documentation: document.getElementById('idUnits').value='\℃';
 <input type="button" id="idUnits">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM