如何用js点击一个div来显示另外一个div

如题所述

这个到是不难,代码如下

<div onclick="showOtherDiv()" style="border:1px solid #fff">点击显示另一个div</div>
<div id="otherDiv" style="display:none;border:1px solid red">点击后显示的div</div>

<script type="text/javascript">

function showOtherDiv(){
//获取要显示的div对象
var  otherDiv=document.getElementById('otherDiv');
//显示
otherDiv.style.display="block";
}

</script>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-12-02

html:

<div style="width:100px; border:1px solid red; text-align:center;" id="div1">
    Click Me
</div>
<div style="width:100px; border:1px solid skyblue; height:100px;" id="div2"></div>

JS:

document.getElementById("div1").onclick = function(){
    document.getElementById("div2").style.display = "none";
}

相似回答