[英]Function not working properly. Please help
Hello I'm having trouble with the function setUpTranslation(). 您好,我在使用功能setUpTranslation()时遇到麻烦。 //The purpose of this function is to place the French phrases into the document and set up the event handlers for the mousedown and mouseup events.
//此功能的目的是将法语短语放入文档中,并为mousedown和mouseup事件设置事件处理程序。
//These are the arrays of the French phrases and English phrases that I have do place into the document: //这些是我在文档中放置的法语短语和英语短语的数组:
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.";
//function I'm having trouble with //功能我遇到了麻烦
function setUpTranslation(){
var phrases = document.getElementByTagName("p");
for (i =0; i<phrases.length; i++){
phrases[i].number =i;
phrases[i].childNodes[1].innerHTML =french[i];
phrases[i].childNodes[1].onmousedown =function(){
swapFE(event);
phrases[i].childNodes[1].onmouseup =function(){
swapEF(event);
};
};
}
//Below are the other two functions swapFE() and swapEF(). //下面是另外两个函数swapFE()和swapEF()。 The purpose of the function swapFE() is to exchange the French phrase for the English translation //The purpose of the function swapEF() is to exchange the English translation for the French phrase.
函数swapFE()的目的是将法语短语替换为英语翻译// //函数swapEF()的目的是将英语翻译替换为法语。
function swapFE(e){
var phrase =e.srcElement;
var parent =phrase.parentNode;
var idnum =parent.childNodes[0];
var phrasenum =parseInt(idnum.innerHTML)-1;
phrase.innerText =english[phrasenum];
}
function swapEF(e){
var phrase =e.srcElement;
var parent =phrase.parentNode;
var idnum =parent.childNodes[0];
var phrasenum =parseInt(idnum.innerHTML)-1;
phrase.innerText =french[phrasenum];
}
//Not sure if these are right. //不确定这些是否正确。 Thanks in advance!
提前致谢!
Assuming that your HTML looks like this 假设您的HTML看起来像这样
<p><span>1</span><span></span></p>
<p><span>2</span><span></span></p>
...
<p><span>10</span><span></span></p>
Then all you need to do is to add the curly bracket after swapFE(event);
然后,您要做的就是在
swapFE(event);
之后添加大括号swapFE(event);
(points for Mr Plunkett) and replace getElementByTagName
with getElementsByTagName
(you're missing an 's' in there). (指向Plunkett先生的点),并将
getElementByTagName
替换为getElementsByTagName
(您在其中缺少“ s”)。
One additional thing to note: If the English phrase is shorter than the French, the container might shrink when the onmousedown
event fires. 需要注意的另一件事:如果英语短语比法语短,则在
onmousedown
事件触发时容器可能会收缩。 If this shrinkage causes the mouse cursor to be positioned outside the container, the subsequent onmouseup
event will not be triggered. 如果此收缩导致鼠标光标定位在容器外部,则不会触发后续的
onmouseup
事件。 Of course, if you are using block elements (eg a <div>
) instead of my assumed <span>
, that likely isn't an issue. 当然,如果您使用的是块元素(例如
<div>
)而不是假定的<span>
,则可能不是问题。 In any case, it's probably better to attach the event listeners to the <p>
tags instead. 无论如何,最好将事件侦听器附加到
<p>
标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.