简体   繁体   English

使用jQuery / JavaScript警告特殊字符

[英]Alerting Special Characters using jQuery/JavaScript

How do i display a string with Special Characters like € in a Javascript/jQuery alert? 如何在Javascript / jQuery警报中显示带有特殊字符(如€)的字符串?

eg: I want to display a message box with "The Price is €10" 例如:我想显示一个“价格为10欧元”的消息框

But when i use the below code: 但是,当我使用以下代码时:

alert("The Price is €10");

The Output shown in the message box is "The Price is €10" , I want my output to be "The Price is €10" . 消息框中显示的输出是"The Price is €10" ,我希望我的输出为"The Price is €10"

Can some help me with this please? 有人可以帮我这个吗? Thanks in advance. 提前致谢。

Use this as the alert. 使用此作为警报。 Works fine for me. 对我来说很好。

alert(' The Price is \u20AC 10');

The description is here : http://leftlogic.com/projects/entity-lookup/ 描述如下: http//leftlogic.com/projects/entity-lookup/

The native alert method does not decode HTML encoded entities. 本机alert方法不解码HTML编码实体。

But browsers do while rendering HTML. 但浏览器在渲染HTML时会这样做。 One hack is to create a HTML element with the specific text as its innerHTML (so it does the character processing), then get back its text property and alerting it out. 一个hack是创建一个HTML元素,其特定文本作为innerHTML (因此它进行字符处理),然后返回其text属性并提醒它。

function alertSpecial(msg) {
    msg = $('<span/>').html(msg).text();
    alert(msg);
}
alertSpecial('The Price is &euro;10');

This will work for all &xxx characters that the browser can display, without needing to find out the character code for each special character you may want to use. 这适用于浏览器可以显示的所有&xxx字符,而无需查找您可能想要使用的每个特殊字符的字符代码。

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

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