<head>
<script>
function createTab(colInput, rowInput){
try{
var cols = parseInt(document.getElementById(colInput).value)
var rows = parseInt(document.getElementById(rowInput).value)
}catch(e){
alert('请输入两个正整数!')
return false
}
var tab = document.createElement('TABLE')
tab.style.border = '1px solid black'
tab.style.borderCollapse='collapse'
for(var i = 0i <rows i++){
var tr = document.createElement('TR')
tab.appendChild(tr)
for(var j = 0j<cols j++){
var td = document.createElement('TD')
td.style.width = parseFloat(1/cols)*window.screen.width+'px'
td.style.border = '1px solid black'
td.style.height = '20px'
tr.appendChild(td)
}
}
document.body.appendChild(tab)
}
</script>
</head>
<body>
<input type="text" id="rows"/>
<input type="text" id = "cols">
<input type="button" onclick="createTab('rows','cols')"/>
</body>
</html>
<form name="form1"><input id="txt1" name="A1" type="text" value="" />
<input id="txt2" name="A2" type="text" value="" />
<input id="txt3" name="A3" type="text" value=""/>
<input id="txt4" name="A4" type="text" value="" onfocus="sum()"/>
</form>
<script type="text/javascript">
function sum(){
var n1 = document.getElementById("txt1").value
var n2 = document.getElementById("txt2").value
var n3 = document.getElementById("txt2").value
var n4
n4 = toInt(n1)+toInt(n2)+toInt(n3)
document.getElementById("txt4").value = n4
}
function toInt(n){
if(parseFloat(n)){
n = n
}
else{
n = 0
}
return parseFloat(n)
}
</script>
--------------------
NaN:Not a Num (不是一个数字)
parseFloat:转换为 Float 类型
parseFloat('12.1w') 返回 12.1
parseFloat('w2') 返回 NaN
类似函数 parseInt 转换为 Int 类型