我的代碼適用於IE,但不適用於Firefox。我試圖讓onmousedown和onmouseup事件在IE瀏覽器以外的瀏覽器中工作,以及函數swapFE,swapEF和setUpTranslation。我基本上有瀏覽器加載時出現的法語短語,當我用鼠標點擊一個短語時,他們應該翻譯成英語,當我放開鼠標時,他們應該回到法語。這是基本的想法。任何其他問題都可以隨意問。JavaScript中的非IE瀏覽器兼容性所需的幫助
你可以看看下面的代碼,並提出我對非IE瀏覽器做錯的建議。如果我取消註釋掉正確的行,但它似乎不適用於Firefox,我可以將它用於IE。
有什麼建議嗎?
/*
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
//I noticed that the function is referenced here as swapFE(event) and the function is written as swapFE(e)...does that make a difference in what shows up??
//phrases[i].childNodes[1].onmousedown = function() { swapFE(event); };
phrases[i].childNodes[1].onmousedown = swapFE;
//phrases[i].childNodes[1].onmouseup = function() { swapEF(event); };
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 = 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";
}
@ ChaosPandion-我仍在學習HTML,但我不知道你在說什麼。你能爲我解釋一下嗎?另外,我會在哪裏插入此功能?你是說在你寫的函數中把「in practice」var節點代替var node = null嗎? – Ashley 2010-02-04 22:44:58
@Ashley - 我會繼續爲你解答。你能否提出更明確的問題?我的答案可能與我無關,因爲我不得不猜測你的問題實際上是什麼。 – ChaosPandion 2010-02-04 23:19:03
我的代碼適用於IE,但不適用於Firefox。我試圖讓onmousedown和onmouseup事件在IE瀏覽器以外的瀏覽器中工作,以及函數swapFE,swapEF和setUpTranslation。所以我想讓別人看我的代碼,看看我可能做錯了什麼,指向了正確的方向。我基本上有瀏覽器加載時出現的法語短語,當我用鼠標點擊一個短語時,他們應該翻譯成英語,當我放開鼠標時,他們應該回到法語。這是基本的想法。任何其他問題都可以隨意問。 – Ashley 2010-02-05 04:40:53