[英]Why can't I get this entity code to display correctly in a browser?
I'm trying to code a UK Pound symbol to be written to a document by JavaScript, but it's code is not being translated and is instead displayed as entered. 我正在尝试编码要通过JavaScript写入文档中的英镑符号,但是该代码未进行翻译,而是按输入显示。
See this JSBin http://jsbin.com/orocox/1/edit 请参阅此JSBin http://jsbin.com/orocox/1/edit
This is the JavaScript: 这是JavaScript:
$("#price").text('£ 1.99');
This is the html: 这是html:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<span id="price"></span>
</body>
</html>
This is the result: '£(;) 1.99' 结果是:'&pound(;)1.99'
*Note that the parenthesis around the ';' *请注意,“;”周围的括号 are added by me to prevent the entity code from being translated on StackOverflow, but they do not exist in the actual output.
由我添加以防止实体代码在StackOverflow上翻译,但实际输出中不存在。
The result I want is: '£ 1.99'. 我想要的结果是:“£1.99”。
use unicode instead: jsbin 使用unicode代替: jsbin
$("#price").text('\u00A3 1.99');
explanation: the £
说明:
£
is an html entity and is not processed as normal text. 是html实体,不会作为普通文本处理。 but unicode works for any text.
但是unicode适用于任何文本。 since you are using
text
it is processed as a string not an html. 由于您使用的是
text
,因此将其作为字符串而非html处理。
Try $("#price").html('£ 1.99');
尝试
$("#price").html('£ 1.99');
instead. 代替。
Use the character itself: 使用角色本身:
$("#price").text('£ 1.99');
This is good for the readability of your code. 这有利于代码的可读性。 How you type “£” depends on your editing environment.
键入“£”的方式取决于您的编辑环境。 Eg, on Windows, you can produce it by typing Alt 0163 if you cannot find any more convenient way (depending on keyboard, keyboard layout, and editor being used).
例如,在Windows上,如果找不到更方便的方式(取决于所使用的键盘,键盘布局和编辑器),则可以通过键入Alt 0163来生成它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.