好的,我正在尝试将里程转换为公里,并且必须在javascript中双向进行。 我开始的线条非常少,而且此刻我很难过,我不知道如何继续。这就是我现在拥有的可怜代码。
Var = x;
x = prompt("Enter Miles to be converted into kilometeres");
x = parseInt(x);
如果有什么东西试图在脑子里想出怎样做的想法。我想帮助自己,但我现在要求提出想法。如果它过了星期三,我可能会要求全力帮助。
答案 0 :(得分:0)
希望此代码段有用
document.addEventListener('click',function(event){ // listening to click event
var target = event.target.id; //Checking id of clicked button
var convertValue = 0; // Initializing 0 value
if(target == "demMiles" || target == "demKm"){ // If button is clicked
var distance = prompt("Please enter distance"); //Will open prompt window
if(distance != null){ // If prompt window is not empty
if(target =="demMiles"){ // Checking for mile to km conversion
convertValue = distance *1.6;
}
else if(target =="demKm"){ // checking for km to mile conversion
convertValue = distance /1.6;
}
document.getElementById("dist").innerHTML =
"Converted distance is " +convertValue; // Displaying the value
}
}
})