我有一个java脚本函数:
// braces required to disambiguate between Function and Consumer
Retryable<String,Void> c = Retryable.of(e->{}, e ->{}, 3,
str -> { System.out.println(str); });
使用div和按钮:
function showPrerequisites() {
var x = document.getElementById('prerequisites');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
这样可以正常工作并且div被隐藏并按预期显示,但是,我想默认隐藏div,所以我将<button class="show-prerequisites" onclick="showPrerequisites()">Show/Hide Prerequisites</button>
<div class="prerequisites" id="prerequisites">
添加到我的CSS中:
display: none;
当我这样做时,第一次按下按钮不显示div,我必须按两次才能显示它。我做错了什么?
答案 0 :(得分:1)
function showPrerequisites() {
var x = document.getElementById('prerequisites');
var style = window.getComputedStyle(x);
var display = style.getPropertyValue('display');
if (display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
&#13;
.prerequisites {
background-color: #ededed;
margin : 20px auto;
border: solid #e6e6e6 thin;
border-radius: 5px;
padding: 3%;
font-weight: 300;
display: none;
}
&#13;
<button class="show-prerequisites" onclick="showPrerequisites()">Show/Hide Prerequisites</button>
<div class="prerequisites" id="prerequisites">
&#13;