简体   繁体   English

为什么我不能使该实体代码在浏览器中正确显示?

[英]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: '&pound(;) 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 &pound; 说明: &pound; 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处理。

check this page's encoding reference : here 检查此页的编码参考: 这里

Try $("#price").html('&pound; 1.99'); 尝试$("#price").html('&pound; 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.

相关问题 为什么我不能在服务器端正确获取formdata? - Why can't I get the formdata correctly on the server side? 如何使用JQuery正确发现和打印浏览器窗口宽度? 为什么它不起作用? - How can I correctly discover and print the browser window width using JQuery? Why it can't work? 我怎样才能让 Javascript 显示我的 JavaScript 代码中的错误,为什么我的提交按钮不起作用? - How can I get Javascript to display the errors that are in my JavaScript code and why doesn't my submit button work? 为什么我无法在JavaScript代码中获得输入值 - Why I can't get input value in my javascript code 为什么我无法使此jquery代码正常工作? - Why I can't get this jquery code to work? 在浏览器中运行ma代码时,为什么看不到曲线? - Why can't I see the curve when i run ma code in Browser? 我无法使这几行简单的代码正常工作,也不知道为什么 - I can't make these few simple line of code work correctly and have no idea why 为什么我无法在浏览器上发出警报? - Why I can't alert somethings on the browser? 无法使我的Leaflet弹出窗口正确显示 - Can't get my Leaflet popup to display correctly 我是一个javascript初学者,我无法在浏览器上运行此代码 - I'm a javascript beginner and I can't get this code to run on the browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM