Javascript 如何显示由 onclick 事件触发的 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10859930/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:16:40  来源:igfitidea点击:

how to display a div triggered by onclick event

javascripthtmlcss

提问by NullPoiиteя

I have have two divs. I want to display a div( which has other divs inside it ) when the onclickevent is triggered.

我有两个divs。我想在触发事件时显示 a div(里面有其他divs )onclick

Any help or suggestion would be appreciated.

任何帮助或建议将不胜感激。

Mockup of a div containing a submit button and another div

包含提交按钮和另一个 div 的 div 模型

回答by u283863

Here you go:

干得好:

div{
    display: none;
}

document.querySelector("button").addEventListener("click", function(){
    document.querySelector("div").style.display = "block";
});

<div>blah blah blah</div>
<button>Show</button>

LIVE DEMO: http://jsfiddle.net/DerekL/p78Qq/

现场演示:http: //jsfiddle.net/DerekL/p78Qq/

回答by Vishal

You'll have to give an ID to the div you want to show/hide, then use this code:

您必须为要显示/隐藏的 div 提供一个 ID,然后使用以下代码:

html:

html:

<div id="one">
    <div id="tow">
        This is text
    </div>
    <button onclick="javascript:showDiv();">Click to show div</button>
</div>

javascript:

javascript:

function showDiv() {
    div = document.getElementById('tow');
    div.style.display = "block";
}

CSS:

CSS:

?#tow { display: none; }?

Fiddle: http://jsfiddle.net/xkdNa/

小提琴:http: //jsfiddle.net/xkdNa/

回答by NullPoiиteя

If you have the ID of the div, try this:

如果您有 div 的 ID,请尝试以下操作:

  <input type='submit' onclick='$("#div_id").show()'>

回答by Sam

function showstuff(boxid){
   document.getElementById(boxid).style.visibility="visible";
}
<button onclick="showstuff('id_to_show');" />

This will help you, I think.

这会帮助你,我想。