在javascript中显示变量

时间:2015-03-29 18:41:12

标签: javascript html variables document.write

我正在尝试创建一个简单的页面,它接受用户输入的变量,并将其添加到预设变量中。然后,我希望它在页面底部显示答案。我能找到的唯一命令是document.write,除了答案之外,它还是一个完整的空白页面。你能帮我找一个document.write的替代命令吗?这是迄今为止的代码:

<!DOCTYPE html>
<html>
<body>
<script>
var y = 0;
var z = 10;
var x = z;
</script>
<p>
Add Numbers:
</p>
<input type="text" name="inpt1" id="usap1"/>
<br/>
<br/>
<input type="button" value="Submit" onclick="add()"/>
<script>
function add() {
var z = 9
var y = document.getElementById('usap1').value
var x = z;
var z = ( Number(x) + Number(y) );
document.write(z)
}
</script>
</body>

1 个答案:

答案 0 :(得分:0)

溶液:

    <!DOCTYPE html>
<html>
<body>
<script>
var y = 0;
var z = 10;
var x = z;
</script>
<p>
Add Numbers:
</p>
<input type="text" name="inpt1" id="usap1"/>
<br/>
<br/>
<input type="button" value="Submit" onclick="add()"/>
<script>
function add() {
var z = 9
var y = document.getElementById('usap1').value
var x = z;
var z = ( Number(x) + Number(y) );
document.getElementById('theEnd').innerHTML= z;
}
</script>
  <span id="theEnd"></span>
</body>