Alerting Special Characters using jQuery/JavaScript

試著忘記壹切 提交于 2020-06-09 17:14:51

问题


How do i display a string with Special Characters like € in a Javascript/jQuery alert?

eg: I want to display a message box with "The Price is €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".

Can some help me with this please? Thanks in advance.


回答1:


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/




回答2:


The native alert method does not decode HTML encoded entities.

But browsers do while rendering 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.

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.




回答3:


Check :

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

http://jsfiddle.net/Nseum/

Unicode Character 'EURO SIGN' (U+20AC)



来源:https://stackoverflow.com/questions/14622769/alerting-special-characters-using-jquery-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!