我正在阅读一本书,它似乎是正确的,但下面的代码不断给我错误:无法设置属性'nodeValue'为null。代码对我来说很有意义,但我不明白为什么点击清除按钮时无法清除文本值。无法设置属性'nodeValue'null
var clear = function(){
$("miles").firstChild.nodeValue = "";
$("gallons").firstChild.nodeValue = "";
$("mpg").firstChild.nodeValue = "";
}
window.onload = function() {
$("calculate").onclick = calculateMpg;
$("miles").focus();
$("clear").onclick = clear;
}
的Html
<section>
<h1>Calculate Miles Per Gallon</h1>
<label for="miles">Miles Driven:</label>
<input type="text" id="miles"><br>
<label for="gallons">Gallons of Gas Used:</label>
<input type="text" id="gallons"><br>
<label for="mpg">Miles Per Gallon</label>
<input type="text" id="mpg" disabled><br>
<label> </label>
<input type="button" id="calculate" value="Calculate MPG"><br>
<input type="button" id="clear" value="clear"><br>
</section>
更改为$(“英里数”).get(0).firstChild.nodeValue =“”和其他类似。这应该能解决你的问题。 – vishakvkt 2013-04-26 23:39:10
@elclanrs - 我不确定他们是否使用jQuery,因为如果它是jQuery,$(“miles”)'将不起作用。我想知道'$'是否是'document.getElementById()'的同义词。也没有,这个问题上没有jQuery标签。 – jfriend00 2013-04-26 23:39:18
致OP。什么是您的项目中的'$'。它是jQuery吗?或者它是'document.getElementById()'?另外,请向我们展示相关的HTML,以便我们了解您的JavaScript是否适当地定位了正确的对象。 – jfriend00 2013-04-26 23:40:49