Javascript 通过单击 HTML 中的另一个按钮使按钮不可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8557119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a button invisible by clicking another button in HTML
提问by Nasrin hakim Mithila
How can I make a button invisible by clicking another button in HTML?
I have written like below, but it doesn't work.
如何通过单击 HTML 中的另一个按钮使按钮不可见?
我写了如下,但它不起作用。
<input type="button" onclick="demoShow();" value="edit" />
<script type="text/javascript">
function demoShow()
{ document.getElementsByName('p2').style.visibility="hidden"; }
</script>
<input id="p2" type="submit" value="submit" name="submit" />
回答by Sonal Khunt
write this
写这个
To hide
隐藏
{document.getElementById("p2").style.display="none";}
to show
显示
{document.getElementById("p2").style.display="block";}
回答by Ranga Reddy
For Visible:
对于可见:
document.getElementById("test").style.visibility="visible";
For Invisible:
对于隐形:
document.getElementById("test").style.visibility="hidden";
回答by Umesh Patil
getElementById
returns a single object for which you can specify the style.So, the above explanation is correct.getElementsByTagName
returns multiple objects(array of objects and properties) for which we cannot apply the style directly.
getElementById
返回单个对象,您可以为其指定样式。所以,上面的解释是正确的。getElementsByTagName
返回我们不能直接应用样式的多个对象(对象和属性的数组)。
回答by check123
Use the id
of the element to do the same.
使用id
元素的 来做同样的事情。
document.getElementById(id).style.visibility = 'hidden';
回答by Harsh Vardan Gupta
Using jQuery!
使用jQuery!
var demoShow = function(){
$("#p2").hide();
}
But I would recommend you give an id to your button on which you want an action to happen. For example:
但我建议您为您希望执行操作的按钮提供一个 id。例如:
<input type="button" id="p1" value="edit" />
<input type="button" id="p2" value="submit" name="submit" />
<script type="text/javascript">
$("#p1").click(function(){
$("#p2").hide();
});
</script>
To show it again you can simply write: $("#p2").show();
要再次显示它,您可以简单地编写: $("#p2").show();
回答by Mr.T.K
try this
尝试这个
function demoShow() {
document.getElementById("but1").style.display="none";
}
<input type="button" value="click me" onclick="demoShow()" id="but" />
<input type="button" value="hide" id="but1" />
回答by Neminath
$( "#btn1" ).click(function() {
$('#btn2').css('display','none');
});
回答by nimday
Try this
尝试这个
<input type="button" onclick="demoShow()" value="edit" />
<script type="text/javascript">
function demoShow()
{p2.style.visibility="hidden";}
</script>
<input id="p2" type="submit" value="submit" name="submit" />
回答by Gavin Mannion
I found problems with the elements being moved around using some of the above, so if you have objects next to each other that you want to just swap this worked best for me
我发现使用上述一些方法移动元素时存在问题,所以如果你有彼此相邻的对象,你只想交换这对我来说效果最好
document.getElementById('uncheckAll').hidden = false;
document.getElementById('checkAll').hidden = true;
回答by JCOC611
To get an element by its ID, use this:
要通过其 ID 获取元素,请使用以下命令:
document.getElementById("p2")
Instead of:
代替:
document.getElementsByName("p2")
So the final product would be:
所以最终产品将是:
document.getElementsById("p2").style.visibility = "hidden";