我正在尝试制作两个不同时显示的表单。第一个在页面打开时保持可见,但如果用户选择,则应隐藏第一个,第二个可以取而代之。所以这是我的CSS:
#switcher {
float: right;
font-size: 12px;
}
#web_upload {
visibility: hidden;
}
#local_upload {
visibility: visible;
}
这是HTML:
<form action="img_upload.php" id="local_upload" method="post" enctype="multipart/form-data">
<center>
<input type="file" name="file" id="file" />
<br />
<input type="image" name="submit" src="graphics/upload.png" />
</center>
</form>
<form action="url_upload.php" id="web_upload" method="post" method="post">
<center>
<input type="text" name="url" id="url" />
<br />
<input type="image" name="submit" src="graphics/upload.png" />
</center>
</form>
这是我的Javascript:
function showHide(id, other)
{
if(document.getElementById(id)) {
if(document.getElementById(id).style.visibility != "hidden") {
document.getElementById(other).style.visibility = "hidden";
document.getElementById(id).style.visibility = "visible";
} else {
document.getElementById(id).style.visibility = "hidden";
document.getElementById(other).style.visibility = "visible";
}
}
}
所以,我有三个问题:
我如何解决这个问题?
答案 0 :(得分:4)
display: none/block;
显示表单/完全隐藏并清除空格
visibility: hidden
;隐藏表单,但保留空间
答案 1 :(得分:4)
@Nathan Campos:我将display
和visibility
合并为一样 -
<强> CSS:强>
#web_upload {
display: none;
visibility: hidden;
}
#local_upload {
display: inline;
visibility: visible;
}
<强> JavaScript的:强>
function showHide(id, other)
{
var id1 = document.getElementById(id);
var id2 = document.getElementById(other);
if (id1.style.display == "none") {
id1.style.display = "inline";
id1.style.visibility = "visible";
id2.style.display = "none";
id2.style.visibility = "hidden";
} else if (id1.style.display == "" || id1.style.display == "inline") {
id1.style.display = "none";
id1.style.visibility = "hidden";
id2.style.display = "inline";
id2.style.visibility = "visible";
}
}
答案 2 :(得分:3)
CSS visibility
属性不是正确的选择。
'visibility'属性指定是否呈现元素生成的框。不可见的框仍会影响布局(将'display'属性设置为'none'以完全禁止框生成)
参考:http://www.w3.org/TR/CSS21/visufx.html#visibility
请考虑应用于元素的CSS display
属性 - display:none
将使其看起来好像根本不存在,它将是不可见的,不会影响布局。
#switcher {
float: right;
font-size: 12px;
}
#web_upload {
display:none;
}
#local_upload {
display:block;
}
//
function showHide(id, other)
{
switch (document.getElementById(id).style.display) {
case 'block':
document.getElementById(id).style.display = 'none';
document.getElementById(other).style.display = 'block';
case 'none':
document.getElementById(id).style.display = 'block';
document.getElementById(other).style.display = 'none';
}
}