<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
var t1=document.getElementById("t1");
var x1=t1.value;
var t2=document.getElementById("t2");
var x2=t2.value;
var sum=x1+x2;
var s1=document.getElementById("s1");
s1.innerHTML=sum.toString();
</script>
</head>
<body>
<input id="t1" type="text" value="12" />+<input id="t2" type="text" value="65" />=<span id="s1"></span>
</body>
</html>
都报错了好不好,代码要放到dom后面或者window.onload里面执行,要不你放前面按循序解析执行代码时dom还没生成,而且要转为数字,否则是value是字符串,就变字符串相加了
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<input id="t1" type="text" value="12" />+<input id="t2" type="text" value="65" />=<span id="s1"></span>
<script>
var t1 = document.getElementById("t1");
var x1 = parseInt(t1.value);///////////////
var t2 = document.getElementById("t2");
var x2 = parseInt(t2.value);//////////////
var sum = x1 + x2;
var s1 = document.getElementById("s1");
s1.innerHTML = sum.toString();
</script>
</body>
</html>