我的代碼哪錯(cuò)了為什么不出來?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Math </title>
<script type="text/javascript">
function mathf(){
? ? var num1,num2,num;
? ? num1 = document.getElementById("num1").value;
? ? num2 = document.getElementById("num2").value;
? ? num = ?Math.floor(num1);
? ? num2 = num;
? ? }
</script>
</head>
<body>
<input type="text" id="num1" /><br/>
<button onClick="mathf()">點(diǎn)擊</button><br/>
<input type="text" id="num2" />
</body>
</html>
2016-05-26
//把自定義的函數(shù)修改下
function mathf(){
? ? var num1,num2,num;
? ? num1 = document.getElementById("num1").value;
? ?// num2 = document.getElementById("num2").value;
? ? num = ?Math.floor(num1);
? ?// num2 = num;
document.getElementById("num2").value=num;
? ? }
2016-07-16
1、事件是小寫——onclick
2、函數(shù)還可以再精簡(jiǎn)——
function mathf(){
????//獲取輸入的數(shù)值
?????var??num1 = document.getElementById("num1").value; ? ? ?
?????//將獲取到的值向下取整后,直接賦給num2
????document.getElementById("num2").value=Math.floor(num1); ?
}
2016-05-26
num2 = document.getElementById("num2").value;這句話是讀取id為num2所在的標(biāo)簽的value ,是一個(gè)值!
num2 = num; 也只是把num的值賦給num2;num2是變量,
var i =1; ? ?i=2; ?最終i=2,難道會(huì)變成1=2?
2016-05-26
function mathf(){
? ? var num1,input2,num;
? ? num1 = document.getElementById("num1").value;
? ? input2 = document.getElementById("num2");
? ? num = ?Math.floor(num1);
? ? input2.value = num;
? ? }
document.getElementById("num2")是得到一個(gè)dom對(duì)象,是一個(gè)引用,把他對(duì)應(yīng)到一個(gè)變量里,通過給這個(gè)dom對(duì)象的value值重新賦值,就能改變輸入框的值了。
document.getElementById("num2").value是得到一個(gè)dom對(duì)象的屬性value的值,獲取的只是一個(gè)數(shù)值而已,你改變了這個(gè)數(shù)值,并不會(huì)改變對(duì)象本身
2016-05-26
//或者把自定義函數(shù)改成這樣
function mathf(){
? ? var num1,num2,num;
? ? num1 = document.getElementById("num1").value;
? ? num2 = document.getElementById("num2");
? ? num = ?Math.floor(num1);
? ? num2.value = num;
? ? }
2016-05-26
var num1,num2;
? ? num1 = document.getElementById("num1").value;
num2 = ?Math.floor(num1);
? ? document.getElementById("num2").value=num2;
2016-05-26
你想要什么樣的結(jié)果???