大神們這段代碼到底是怎么運(yùn)算的???
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>style樣式</title>
<script type="text/javascript">
var numresult;
var str;
function onclicknum(nums) {
?? str = document.getElementById("nummessege");
?? str.value = str.value + nums;
}
</script>
</head>
<body>
<input type="text" id="nummessege" />
<input type="button" value="4" id="4" onclick="onclicknum(4)">
</body>
</html>
str = document.getElementById("nummessege");
?? str.value = str.value + nums; 這段代碼有點(diǎn)不理解
2016-03-20
str.value就是你文本框中輸入的數(shù)
整個代碼就是給文本框輸入的數(shù)加上一個給定的num值,代碼中num值固定是4
但是你給的代碼只是鏈接兩個字符而已,并不是做數(shù)值運(yùn)算,如果需要數(shù)值運(yùn)算的話,函數(shù)代碼應(yīng)該改成
str = document.getElementById("nummessege");
? ?str.value = parseInt(str.value) + parseInt(nums);
其中parseInt的作用是強(qiáng)制把里面的參數(shù)轉(zhuǎn)換成Int整型,這樣才能進(jìn)行數(shù)值運(yùn)算