[英]Show div element only on clicking the button
我试过下面的代码来隐藏/显示div元素。 现在我想隐藏页面加载中的元素。 div元素仅应在单击按钮时显示。
<style type="text/css">
#sectiontohide {
padding:20px;
background:#f0f0f0;
width:400px;
}
</style>
<script type="text/javascript">
function toggle_div(id) {
var divelement = document.getElementById(id);
if(divelement.style.display == 'none')
divelement.style.display = 'block';
else
divelement.style.display = 'none';
}
</script>
</head>
<body>
<h3>JavaScript to show/hide a Div</h3>
<button onClick="toggle_div('sectiontohide');">Click here</button> to toggle visibility of div element #sectiontohide
<br /><br />
<div id="sectiontohide">This is the div to hide with id = sectiontohide</div>
只需将CSS设置为隐藏即可
<style type="text/css">
#sectiontohide {
padding:20px;
background:#f0f0f0;
width:400px;
display:none;
}
</style>
看到您正在使用内联样式,为什么不只是将其设置为开始呢?
<div id="sectiontohide" style="display: none;">This is the div to hide with id = sectiontohide</div>
使div的可见隐藏选项。
<div id="sectiontohide" style='display:none'>This is the div to hide with id = sectiontohide</div>
您可以在脚本块上尝试此代码
window.onload = function () {
toggle_div("sectiontohide");
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.