Javascript JQuery:如何文本插入 HTML ascii 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2726935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:39:37  来源:igfitidea点击:

JQuery: How to text insert HTML ascii character?

javascriptjquery

提问by JGreig

I have the following JQuery code:

我有以下 JQuery 代码:

$(this).text('Options ▴');

However, the ascii code isn't showing up on my web page. All that shows up is on the webpage is Options ▴.

但是,ascii 代码没有显示在我的网页上。所有显示在网页上的是Options ▴

What am I doing wrong?

我究竟做错了什么?

回答by Fyodor Soikin

Use the .html()method instead of .text().

使用.html()方法而不是.text()

The whole point of .text()method is to make the text appear exactly as it's in the string, with all tags and entities.

整点.text()的方法是使文本显示,正是因为它是在字符串中,所有的标签和实体。

回答by kennebec

If you use a unicode escape ('\u25B2') instead of the html escape character, you can use the .text method.

如果您使用 unicode 转义符 ('\u25B2') 而不是 html 转义符,则可以使用 .text 方法。

Some users will not have a font that will display either version.

某些用户将没有可显示任一版本的字体。

回答by user791341

..or if someone wants to use just javascript:

..或者如果有人只想使用javascript:

var d = document.createElement("div");
d.appendChild(document.createTextNode("500"));
var euro = document.createElement("span");
euro.innerHTML = "€"; //your character
d.appendChild(euro);

prints: 500

印数:500

回答by Alex Martelli

&#x25B4is definitely notascii (ascii's a character code that runs from 0 to 127...!-) -- it seems to be a high-page unicode character. What char encoding is your page using? With something "universal" like utf-8, unicode characters shouldshow up... if the browser has them in its font, of course. With other encodings, such characters might just be impossible to transmit and show.

&#x25B4绝对不是ascii(ascii 是从 0 到 127 的字符代码......!-)——它似乎是一个高页 unicode 字符。您的页面使用什么字符编码?对于像 utf-8 这样的“通用”字符,unicode 字符应该显示出来……当然,如果浏览器的字体中有它们。使用其他编码,此类字符可能无法传输和显示。

回答by Krunal

You can try this:

你可以试试这个:

$(this).html('Options ▴');

Output:

输出:

Options ?

If you want to test it online you can check out this website. (I liked it very much)

如果你想在线测试它,你可以查看这个网站。(我很喜欢它)