~~~
<div>
<span>值1:</span>
<input id="input1" type="text />
</div>
<div>
<span>值2:</span>
<input id="input2" type="text />
</div>
<div>
和是:
<span id="amount”>0</span>
</div>
var oInp1 = document.getElementById("input1");
var oInp2 = document.getElementById("input2");
var oAmount = document.getElementById("amount");
if("oninput" in oInp1){
oInp1.addEventListener("input",getWord,false);
} else {
oInp1.onpropertychange = getWord;
}
if("oninput" in oInp2){
oInp2.addEventListener("input",getWord,false);
} else {
oInp2.onpropertychange = getWord;
}
//判断值是否为空,怎么显示,避免出现“NaN”
function getWord(){
if(oInp1.value == "" && oInp2.value == "") {
oAmount.innerHTML = "0";
} else if (oInp2.value == "") {
oAmount.innerHTML = "0";
} else if(oInp1.value == ""){
oAmount.innerHTML = parseInt(oInp2.value)
} else {
oAmount.innerHTML = parseInt(oInp2.value) + parseInt(oInp1.value);
}
~~~