<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函数调用</title>
<script type="text/javascript">
function contxt() //定义函数
{
var {
Int p1 = document.getElementById("1").value;
Int p2 = document.getElementById("2").value;
Int sum = p1 + p2;
}
document.write(sum);
alert("哈哈,调用函数了!");
}
</script>
</head>
<body>
<form>
<input type="text" id="1"/>
<input type="text" id="2"/>
<input type="button" value="点击我" onclick=" contxt() " />
</form>
</body>
</html>
老哥,这是js不是java,要拿到input框的内容可以用value拿到
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函数调用</title>
<script type="text/javascript">
function contxt() //定义函数
{
var p1 = Number(document.getElementById("1").value);
var p2 = Number(document.getElementById("2").value);
var sum = p1 + p2;
document.write(sum);
alert("哈哈,调用函数了!");
}
</script>
</head>
<body>
<form>
<input type="text" id="1"/>
<input type="text" id="2"/>
<input type="button" value="点击我" onclick=" contxt() " />
</form>
</body>
</html>
function里面的格式错了
还有这样输出的结果是字符连接串 不是数字计算