为什么单击Go按钮后文本字段和按钮会消失
Why does the textfields and button disappears after clicking the Go button?
我在这里有我的代码(html/javascript),它允许用户输入正方形的大小和颜色。问题是当我点击go按钮时,文本字段会消失,包括按钮,显示的是输出。有没有什么方法可以让我保留文本字段和按钮,然后输出它下面的方块?
<html>
<head>
<script type="text/javascript">
size=0;
function display(form){
size=form.inputbox.value;
color=form.inputbox2.value;
for(i=0;i<size;i++){
for(j=0;j<size;j++){
if(i==0||i==(size-1)){
document.write("<font color="+color+">* ");
}
else if (j == 0||j == (size-1)) {
document.write("* ");
}
else {
document.write(" ");
}
}
document.write("<br>");
}
}
</script>
</head>
<body>
<form name="form1" action="" method="get">
Input size of the square:
<input type="text" name="inputbox" value=""><br>
Input color of the square:
<input type="text" name="inputbox2" value="">
<input type="button" name="button" value="Go" onClick="display(this.form)">
</form>
</body>
</html>
在已完成渲染的页面上调用document.write
将破坏页面上现有的DOM文档,仅用新编写的HTML元素替换它。
如果要添加到现有的DOM元素,请选择一个元素并写入其innerHTML
属性。
<html>
<head>
<script type="text/javascript">
size=0;
function display(form){
size=form.inputbox.value;
color=form.inputbox2.value;
// A node to write into
writeTo = document.getElementById('writeTo');
for(i=0;i<size;i++){
for(j=0;j<size;j++){
if(i==0||i==(size-1)){
writeTo.innerHTML = writeTo.innerHTML + "<font color="+color+">* ";
}
else if (j == 0||j == (size-1)) {
writeTo.innerHTML = writeTo.innerHTML + "* ";
}
else {
writeTo.innerHTML = writeTo.innerHTML + " ";
}
}
writeTo.innerHTML = writeTo.innerHTML + "<br>";
}
}
</script>
</head>
<body>
<form name="form1" action="" method="get">
Input size of the square:
<input type="text" name="inputbox" value=""><br>
Input color of the square:
<input type="text" name="inputbox2" value="">
<input type="button" name="button" value="Go" onClick="display(this.form)">
</form>
<div id='writeTo'></div>
</body>
</html>
并在HTML中添加一个空节点,比如:
<div id='writeTo'></div>
这是在jsFiddle.net 上运行的
要使正方形都是正确的颜色,最简单的方法是更改<div id='writeTo'>
的CSS颜色属性
document.getElementById('writeTo').style.color = color;
相关文章:
- 如何显示没有按钮且在指定时间段后消失的消息框
- 单击按钮显示重新单击消失
- 为什么当我在 jQuery 中添加切换事件时我的按钮消失了
- jQuery滑入和滑出行为奇怪,按钮消失
- 如何使HTML中的按钮消失
- 当我错过中间的单选按钮时,以前的正确单选按钮消失了
- 访问cookie时,提交按钮消失
- 使用facebook作为(备用用户帐户)导致点赞按钮消失
- Javascript按钮消失
- 即使在下次访问时,点击时也会使按钮消失
- 更新面板后,Facebook共享按钮消失
- 按钮消失时,点击随机图像更改
- 显示分数后按钮消失
- 单选按钮消失'
- 显示/隐藏按钮消失
- 使按钮消失,并重新出现在添加/删除类
- 改变State UI Routing后按钮消失
- 如果我在ie9中刷新页面,Facebook登录按钮消失
- 为什么按钮消失了
- 使图像出现,而其他图像通过单击按钮消失