Javascript 为什么我不能在 HTML 中显示磅 (£) 符号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4382518/
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 12:11:27  来源:igfitidea点击:

Why can't I display a pound (£) symbol in HTML?

javascripthtml

提问by Stephen Oberauer

I'm trying to display the pound symbol in HTML (from PHP) but all I get is a symbol with a question mark.

我试图在 HTML(来自 PHP)中显示英镑符号,但我得到的只是一个带问号的符号。

The following are things that I've tried.

以下是我尝试过的事情。

In PHP:

在 PHP 中:

header('Content-type: text/html; charset=utf-8');

In HTML, put this in the head tag:

在 HTML 中,把它放在 head 标签中:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

I tried displaying it using a javascript function which converts it to:

我尝试使用 javascript 函数显示它,该函数将其转换为:

&amp;#65533;

I suppose it would help if I knew what I was doing... but I guess that's why I'm asking this question :)

我想如果我知道我在做什么会有所帮助...但我想这就是我问这个问题的原因:)

采纳答案by Pekka

Educated guess: You have a ISO-8859-1 encoded pound sign in a UTF-8 encoded page.

有根据的猜测:您在 UTF-8 编码的页面中有一个 ISO-8859-1 编码的英镑符号。

Make sure your data is in the right encoding and everything will work fine.

确保您的数据采用正确的编码,一切正常。

回答by Ratch90

Use &#163;. I had the same problem and solved it using jQuery:

使用&#163;. 我遇到了同样的问题并使用 jQuery 解决了它:

$(this).text('&amp;#163;');
  • If you try this and it does not work, just change the jQuery methods,
  • 如果您尝试这样做但它不起作用,只需更改 jQuery 方法,
$(this).html('&amp;#163;');
  • This always work in all contexts...
  • 这始终适用于所有情况......

回答by ismael Miguel

1st: the pound symbol is a "special" char in utf8 encoding (try saving £$ in a iso-8859-1 (or iso-8859-15) file and you will get ? when encoding using header)

第一:英镑符号是 utf8 编码中的“特殊”字符(尝试将 £$ 保存在 iso-8859-1(或 iso-8859-15)文件中,您将在使用标头编码时得到 ?)

2nd: change your encoding to utf8 form the file. there are plenty of methods to do it. notepad and notepad++ are great sugestions.

第二:将您的编码从文件中更改为 utf8。有很多方法可以做到。记事本和记事本++是很好的建议。

3rd: use ob_start(); (in php) BEFORE YOU MAKE ANY OUTPUT if you are getting weird encoding errors, like missing the encoding sometimes. and YES, this solves it! this kind of errors occurs when a page is encoded in windows-1252(ANSI),ASCII,iso-8859-1(5) and then you have all the others in utf8. this is a terrible error and can cause weird things like session_start(); not working.

第三:使用 ob_start(); (在 php 中)在您进行任何输出之前,如果您遇到奇怪的编码错误,例如有时会丢失编码。是的,这解决了它!当页面以 windows-1252(ANSI)、ASCII、iso-8859-1(5) 编码时,会发生这种错误,然后您将所有其他页面编码为 utf8。这是一个可怕的错误,可能会导致诸如 session_start() 之类的奇怪事情;不工作。

4th: other php solutions:

第四:其他php解决方案:

utf8_encode('£');
htmlentities('£');
echo '&amp;pound;';

5th: javascript solutions:

第五:javascript解决方案:

document.getElementById('id_goes_here').innerText.replace('£','&amp;pound;');
document.getElementById('id_goes_here').innerText.replace('£',"\u00A3");
$(this).html().replace('£','&amp;pound;'); //jquery
$(this).html().replace('£',"\u00A3"); //jquery
String.fromCharCode('163');

you MUST send £, so it will repair the broken encoded code point. please, avoid these solutions! use php! these solutions only show how to 'fix' the error, and the last one only to create the well-encoded char.

你必须发送£,这样它就会修复损坏的编码代码点。请避免这些解决方案!使用PHP!这些解决方案仅显示如何“修复”错误,而最后一个仅用于创建编码良好的字符。

回答by nc3b

Have you tried displaying a &pound;?

您是否尝试过显示&pound;?

Hereis an overwhelming list.

是一个压倒性的清单。

回答by Brian Campbell

You could try using &pound;or &#163;instead of embedding the character directly; if you embed it directly, you're more likely to run into encoding issues in which your editor saves the file is ISO-8859-1 but it's interpreted as UTF-8, or vice versa.

您可以尝试使用&pound;&#163;代替直接嵌入字符;如果您直接嵌入它,您更有可能遇到编码问题,您的编辑器保存的文件是 ISO-8859-1,但它被解释为 UTF-8,反之亦然。

If you want to embed it (or other Unicode characters) directly, make sure you actually save your file as UTF-8, and set the encoding as you did with the Content-Typeheader. Make sure when you get the file from the server that the header is present and correct, and that the file hasn't been transcoded by the web server.

如果您想直接嵌入它(或其他 Unicode 字符),请确保您确实将文件保存为 UTF-8,并像对Content-Type标头所做的那样设置编码。确保从服务器获取文件时标头存在且正确,并且该文件尚未被 Web 服务器转码。

回答by dijipiji

Or for other code equivalents try:

或者对于其他等效代码,请尝试:

&#163;
&pound;

回答by Fury

This works in all chrome, IE, Firefox.

这适用于所有 chrome、IE、Firefox。

In Database > table > field type .for example set the symbol column TO varchar(2) utf8_binphp code:

在 Database > table > field type .for example 中将符号列设置为varchar(2) utf8_binphp 代码:

$symbol = '£';
echo mb_convert_encoding($symbol, 'UTF-8', 'HTML-ENTITIES');
or 
html_entity_decode($symbol, ENT_NOQUOTES, 'UTF-8');

And also make sure set the HTML OR XML encoding to encoding="UTF-8"

并确保将 HTML OR XML 编码设置为 encoding="UTF-8"

Note: You should make sure that database, document type and php code all have a same encoding

注意:您应该确保数据库、文档类型和 php 代码都具有相同的编码

How ever the better solution would be using &pound;

更好的解决方案将如何使用 &pound;

回答by István Ujj-Mészáros

You need to save your PHP script file in UTF-8 encoding, and leave the <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />in the HTML.

您需要将 PHP 脚本文件保存为 UTF-8 编码,并将 保留<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />在 HTML 中。

For text editor, I recommend Notepad++, because it can detect and display the actual encoding of the file (in the lower right corner of the editor), and you can convert it as well.

对于文本编辑器,我推荐Notepad++,因为它可以检测并显示文件的实际编码(在编辑器的右下角),并且您也可以对其进行转换。