gpt4 book ai didi

javascript - 在用户给定的范围内将英里转换为公里并将其显示在列表中

转载 作者:太空宇宙 更新时间:2023-11-03 23:45:24 25 4
gpt4 key购买 nike

我需要使用三个文件(Javascript、HTML 和 CSS)编写代码。我不确定我的代码有什么问题,请帮我找出错误。用户将在两个文本区域中写入范围,当单击按钮时,将所有值从第一个给定数字开始转换为第二个给定数字。这是我到目前为止写的:

HTML代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/converter.css"/>
<title>Celsius to Fahrenheit Converter</title>
<script language="JavaScript" src="../js/c2f.js" type="text/javascript">
</script>
</head>
<body>
<h2>Miles to Kilometers Converter</h2>
<form action="">
<p>
<textarea rows="1" name="Input1" id="Input1" cols="10"></textarea>
<textarea rows="1" name="Input2" id="Input2" cols="10"></textarea>
<input type="button" value="Convert" name="B3" onclick="conversionTable()">
<input type="reset" value="Clear" name="B2">
</p>
</form>
<div id="conversion">
</div>
</body>
</html>

JavaScript 代码:

function conversionTable(tagId, from, to)
{
var first = document.getElementById("Input1");
var second = document.getElementById("Input2");
from =first;
to = second;
var conv = document.getElementById(tagId);
var tab = document.createElement("table");
var bod = document.createElement("tbody");
var thed = document.createElement("thead");
tab.appendChild(thed);
tab.appendChild(bod);
var tr = document.createElement("tr");
thed.appendChild(tr);
var th = document.createElement("th");
tr.appendChild(th);
th.appendChild(document.createTextNode("Miles"));
th = document.createElement("th");
tr.appendChild(th);
th.appendChild(document.createTextNode("Kilometers"));
conv.appendChild(tab);



for(var i=from; i<=to; i++){
tr = document.createElement("tr");
if (i%2==0)
tr.setAttribute("class", "even");
else
tr.setAttribute("class", "odd");
bod.appendChild(tr);
td = document.createElement("td");
tr.appendChild(td);
td.appendChild(document.createTextNode(i));
td = document.createElement("td");
tr.appendChild(td);
td.appendChild(document.createTextNode(c2f(i)));
}
function c2f(c) {return Math.round((c * 1.6093)*10)/10}
}

CSS 代码:

h2{text-align:center; color:blue; background: #EFEFEF}
body{margin: 4em; width: 400px}
table{margin: 2em; padding: 1em;}
th{background: #EFEFFF}
tr.even {background: #B8B8B8}
tr.odd {background: #E0FFFF}

再一次,我试图将两个变量(第一个和第二个)传递到我的 conversionTable() 函数中。

最佳答案

DEMO HERE

对您的 html 的更改:

<input type="button" value="Convert" name="B3" onclick="conversionTable('conversion')" />

对您的 js 的更改:

from = parseInt(first.value);
to = parseInt(second.value);

就是这样。它应该符合您的要求。

关于javascript - 在用户给定的范围内将英里转换为公里并将其显示在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com