如何使用 parseInt()函数从短语对象中获取短语编号?我尝试在下面的 var phrasenum 中的 swapFE函数中创建函数,但它不起作用。我认为我的价值观错了。我应该把它放在那里呢?任何帮助将不胜感激。提前致谢!以下是法语短语编号(1-10或JavaScript 0-9)和法语短语:
> 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 setUpTranslation() {
var phrases = document.getElementsByTagName("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); };
}
}
function swapFE(e) {
var phrase = e.srcElement;
var phrasenum = parseInt(phrase[1].childNodes[1].innerText);
alert("French Number = "+phrasenum+"Phrase = "+phrase.childNodes[1]);
}
答案 0 :(得分:3)
使用parseInt
的其他一些陷阱:
// As bryantsai and shinkou mentioned, not using the radix/base
parseInt("08"); // result is 0
parseInt("08", 10); // result is 8
// Thousands separator:
parseInt("1,000"); // result is 1
parseInt("1,000".replace(",","")); // result is 1000
// Foreign decimal separator
parseInt("2,5"); // result is 2;
parseInt("2,5".replace(",",".")); // result is still 2
parseFloat("2,5".replace(",",".")); // result is now 2.5
// Finally, using the (+) operator instead of parseInt/parseFloat
+"2"; // result is 2
+"08"; // result is 8
+"1.5"; // result is 1.5
+"1,000"; // result is NaN - cannot parse thousand separators
+"1,5"; // result is NaN - decimal point must be a .
有时使用+运算符进行转换是一种更好的方法,因为无效的数字字符串将始终提供NaN,而parseInt不是这种情况,如果字符串以1开头,它将始终返回一个数字。
答案 1 :(得分:3)
这是我提出的答案,并且有效。见var phrasenum。谢谢你们的帮助!
//this function changes the French phrase to an English phrase.
function swapFE(e) {
var phrase = e.srcElement;
//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];
}
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];
}
答案 2 :(得分:2)
我不确定您的实施出了什么问题。但是,如果它是parseInt的返回值似乎不正确,也许你应该尝试将字符串与字符串一起包含作为输入。 e.g。
var phrasenum = parseInt(phrase[1].childNodes[1].innerText, 10);
答案 3 :(得分:1)
使用parseInt()
存在一些缺陷。也许您应该粘贴一些无法正常工作的示例。但是,以下示例应该为您提供一些想法(希望如此):
// working examples
parseInt("7"); // 7
parseInt("8"); // 8
parseInt("9"); // 9
// oops ...
parseInt("07"); // 7
parseInt("08"); // 0
parseInt("09"); // 0
// should always give base
parseInt("07", 10); // 7
parseInt("08", 10); // 8
parseInt("09", 10); // 9
另外,检查规格应该有所帮助:15.1.2.2 parseInt(string , radix)
答案 4 :(得分:0)
parseInt
采用数字的文本表示形式,并将数字作为整数输出。
你不想这样做。
将您的代码更改为:
function swapFE(e) {
var phrase = e.srcElement;
alert("French Number = "+this.parent.number+"Phrase = "+phrase.childNodes[1]);
}
在上一个功能中,您设置了phrases[i].number = i;
和phrases[i].childNodes[1].onmousedown = function() { swapFE(event); };
这意味着childNode的父节点包含正在显示的短语的索引。
如果this.parent.number
不起作用,请尝试phrase.number
。
<强>更新强> 这就是我如何实现你想要的:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<title>test page</title>
<script>
var english=[];
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=[];
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 pageLoad()
{
var main = document.getElementById("main");
for(var i = 0; i < french.length; ++i)
{
var p = document.createElement("p");
p.number = i;
var text = document.createTextNode(french[i]);
p.appendChild(text);
hookEvent(p, "mousedown", swapFE);
main.appendChild(p);
}
}
function swapFE()
{
alert("French Number = "+this.number+" Phrase = "+this.firstChild.nodeValue);
}
function hookEvent(element, eventName, callback)
{
if(typeof(element) == "string")
element = document.getElementById(element);
if(element == null)
return;
if(element.addEventListener)
{
if(eventName == 'mousewheel')
element.addEventListener('DOMMouseScroll', callback, false);
element.addEventListener(eventName, callback, false);
}
else if(element.attachEvent)
element.attachEvent("on" + eventName, callback);
}
</script>
</head>
<body onload="pageLoad()">
<div id="main">
</div>
</body>
<html>