3 回答
TA贡献1789条经验 获得超8个赞
这很简单adjacent sibling combinator
,不需要 JavaScript。只需选择任何button
紧邻input[type="checkbox"]
并使其显示block
(而不是inline
)。
input[type="checkbox"] + button {
display: block;
}
<div>
<input type="text" />
<input type = "checkbox" />
<button> click me </button>
</div>
<div>
<input type="text" />
<button> click me </button>
</div>
TA贡献1921条经验 获得超9个赞
您在寻找这样的东西吗?
运行下面的代码片段:
//here we are getting each element by its id
var check = document.getElementById("check");
var input = document.getElementById("input");
var button = document.getElementById("button");
//this is the condition we're using (it could be anything but it was easy to use a boolean for this example)
var bool = false;
//here is the function we're calling on click
function clickMe() {
//when button is clicked we set bool from false to true
bool = true;
//if bool is true we we will apply the following styles to the check and button
if (bool === true) {
check.style.display = "inline-block";
button.style.display = "block";
}
}
#check {
display: none;
}
<input type="checkbox" id="check"><input id="input"><button id="button" onclick="clickMe();">Click Me</button>
TA贡献1886条经验 获得超2个赞
建议切换父级的样式
function toggleClass() {
const element = document.getElementById("container")
element.classList.toggle("toggle");
}
#checkbox {
display:none;
}
.toggle #checkbox {
display:block;
}
<div id="container">
<input type="text"/>
<input id="checkbox" type="checkbox"/>
<button onClick="toggleClass()">Button</button>
</div>
添加回答
举报