JavaScript 中的特殊字符无法在网站上正确显示
当我从法语短语(onload 函数)转到英语短语(onmousedown 函数)并返回法语短语(onmouseup 函数)时,我的 IE 和 Chrome 浏览器无法正确显示法语短语。当我松开特定短语的鼠标时,它会返回法语,但会显示 ô 和 é 的特殊字符(分别是 ô
和 é ) 不从数字代码更改为 ô 和 é。谁能告诉我为什么这样做以及如何解决它?
这是我的 JavaScript 代码:
/*
Function List:
eventSource(e)
Returns the source of an event in either event model
swapFE(phrase, pnum)
Changes a French phrase to the English version of that phrase.
swapEF(phrase, pnum)
Changes an English phrase ot the Frech version of that phrase.
setUpTranslation()
Insert the current week's french phrases into document and set up
event handlers for the phrases.
*/
//Returns the source of an event in either event model
function eventSource(e) {
var IE = document.attachEvent ? true:false;
var DOM = document.addEventListener ? true: false;
if (IE) return event.srcElement;
else if (DOM) return e.currentTarget;
}
//I added the function below to try and make it cross-browser compatible but it did not work....help!
//function applysetUpTranslation(phrases[i],"click",swapFE(e),false) {
//if (IE) phrases[i].attachEvent("on"+onmousedown, swapFE(e));
//else if (DOM) phrases[i].addEventListener(click,swapFE(e),true);
//}
function setUpTranslation() {
var IE = document.attachEvent ? true:false;
var DOM = document.addEventListener ? true: false;
var phrasesContainer = document.getElementById("phrases");
var phrases= phrasesContainer.getElementsByTagName("p");
for (i = 0; i<phrases.length; i++) {
phrases[i].number = i;
phrases[i].childNodes[1].innerHTML = french[i];
phrases[i].childNodes[1].id = i;
//childNodes[1] is the second span in the <p> array
if (IE) {
phrases[i].childNodes[1].onmousedown = function() { swapFE(event); };
} else {
phrases[i].childNodes[1].onmousedown = swapFE;
}
if (IE) {
phrases[i].childNodes[1].onmouseup = function() { swapEF(event); };
} else {
phrases[i].childNodes[1].onmouseup = swapEF;
}
}
}
//this function changes the French phrase to an English phrase.
function swapFE(e) {
var phrase = eventSource(e);
//phrase.innerText = english[phrase.id];
var parent = phrase.parentNode;
//childNodes[0] is the number of the phrase +1
var idnum = parent.childNodes[0];
//parseInt takes a textstring and extracts it to make a number. Then you will subtract 1 from the number.
var phrasenum = parseInt(idnum.innerHTML)-1;
phrase.innerText = english[phrasenum];
parent.childNodes[1].style.fontStyle= "normal";
parent.childNodes[1].style.color = "rgb(155, 102, 102)";
}
function swapEF(e) {
var phrase = eventSource(e);
//var phrase = e.srcElement;
//phrase.innerText = english[phrase.id];
var parent = phrase.parentNode;
var idnum = parent.childNodes[0];
var phrasenum = parseInt(idnum.innerHTML)-1;
phrase.innerText = french[phrasenum];
parent.childNodes[1].style.fontStyle= "italic";
parent.childNodes[1].style.color= "black";
}
这是其中包含法语数组的 js 代码:
var english=new Array();
english[0]="This hotel isn't far from the Eiffel Tower.";
english[1]="What time does the train arrive?";
english[2]="We have been waiting for the bus for one half-hour.";
english[3]="This meal is delicious";
english[4]="What day is she going to arrive?";
english[5]="We have eleven minutes before the train leaves!";
english[6]="Living in a foreign country is a good experience.";
english[7]="Excuse me! I'm late!";
english[8]="Is this taxi free?";
english[9]="Be careful when you go down the steps.";
var french=new Array();
french[0]="Cet hôtel n'est pas loin de la Tour Eiffel.";
french[1]="A quelle heure arrive le train?";
french[2]="Nous attendons l'autobus depuis une demi-heure.";
french[3]="Ce repas est délicieux";
french[4]="Quel jour va-t-elle arriver?";
french[5]="Nous avons onze minutes avant le départ du train!";
french[6]="Habiter dans un pays étranger est une bonne expérience.";
french[7]="Excusez-moi! Je suis en retard!";
french[8]="Est-ce que ce taxi est libre?";
french[9]="Faites attention quand vous descendez l'escalier.";
非常感谢您的帮助。
My IE and Chrome browsers are not displaying the French phrases correctly when I go from a French phrase (onload function) to a English phrase (onmousedown function) and back to a French phrase (onmouseup function). When I let up on the mouse of a particular phrase it goes back to French but the special characters for ô and é (which are ô
and é
) do not change from the number code to the ô and é. Can anyone tell me why it is doing this and how I can fix it?
Here is my JavaScript code:
/*
Function List:
eventSource(e)
Returns the source of an event in either event model
swapFE(phrase, pnum)
Changes a French phrase to the English version of that phrase.
swapEF(phrase, pnum)
Changes an English phrase ot the Frech version of that phrase.
setUpTranslation()
Insert the current week's french phrases into document and set up
event handlers for the phrases.
*/
//Returns the source of an event in either event model
function eventSource(e) {
var IE = document.attachEvent ? true:false;
var DOM = document.addEventListener ? true: false;
if (IE) return event.srcElement;
else if (DOM) return e.currentTarget;
}
//I added the function below to try and make it cross-browser compatible but it did not work....help!
//function applysetUpTranslation(phrases[i],"click",swapFE(e),false) {
//if (IE) phrases[i].attachEvent("on"+onmousedown, swapFE(e));
//else if (DOM) phrases[i].addEventListener(click,swapFE(e),true);
//}
function setUpTranslation() {
var IE = document.attachEvent ? true:false;
var DOM = document.addEventListener ? true: false;
var phrasesContainer = document.getElementById("phrases");
var phrases= phrasesContainer.getElementsByTagName("p");
for (i = 0; i<phrases.length; i++) {
phrases[i].number = i;
phrases[i].childNodes[1].innerHTML = french[i];
phrases[i].childNodes[1].id = i;
//childNodes[1] is the second span in the <p> array
if (IE) {
phrases[i].childNodes[1].onmousedown = function() { swapFE(event); };
} else {
phrases[i].childNodes[1].onmousedown = swapFE;
}
if (IE) {
phrases[i].childNodes[1].onmouseup = function() { swapEF(event); };
} else {
phrases[i].childNodes[1].onmouseup = swapEF;
}
}
}
//this function changes the French phrase to an English phrase.
function swapFE(e) {
var phrase = eventSource(e);
//phrase.innerText = english[phrase.id];
var parent = phrase.parentNode;
//childNodes[0] is the number of the phrase +1
var idnum = parent.childNodes[0];
//parseInt takes a textstring and extracts it to make a number. Then you will subtract 1 from the number.
var phrasenum = parseInt(idnum.innerHTML)-1;
phrase.innerText = english[phrasenum];
parent.childNodes[1].style.fontStyle= "normal";
parent.childNodes[1].style.color = "rgb(155, 102, 102)";
}
function swapEF(e) {
var phrase = eventSource(e);
//var phrase = e.srcElement;
//phrase.innerText = english[phrase.id];
var parent = phrase.parentNode;
var idnum = parent.childNodes[0];
var phrasenum = parseInt(idnum.innerHTML)-1;
phrase.innerText = french[phrasenum];
parent.childNodes[1].style.fontStyle= "italic";
parent.childNodes[1].style.color= "black";
}
Here is the js code with the French array in it:
var english=new Array();
english[0]="This hotel isn't far from the Eiffel Tower.";
english[1]="What time does the train arrive?";
english[2]="We have been waiting for the bus for one half-hour.";
english[3]="This meal is delicious";
english[4]="What day is she going to arrive?";
english[5]="We have eleven minutes before the train leaves!";
english[6]="Living in a foreign country is a good experience.";
english[7]="Excuse me! I'm late!";
english[8]="Is this taxi free?";
english[9]="Be careful when you go down the steps.";
var french=new Array();
french[0]="Cet hôtel n'est pas loin de la Tour Eiffel.";
french[1]="A quelle heure arrive le train?";
french[2]="Nous attendons l'autobus depuis une demi-heure.";
french[3]="Ce repas est délicieux";
french[4]="Quel jour va-t-elle arriver?";
french[5]="Nous avons onze minutes avant le départ du train!";
french[6]="Habiter dans un pays étranger est une bonne expérience.";
french[7]="Excusez-moi! Je suis en retard!";
french[8]="Est-ce que ce taxi est libre?";
french[9]="Faites attention quand vous descendez l'escalier.";
Thank you so much for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要使用
innerHTML
来解释特殊字符:I think you need to use
innerHTML
for the special characters to get interpreted:如果您创建的 HTML/Javascript 代码文件使用 Unicode,那么您应该能够毫无问题地将 ô 和 é 字符放入 Javascript 和 HTML 中。
If your HTML/Javascript code files are created so they use Unicode, you should be able to put the ô and é characters in your Javascript and HTML with no problems.