3 回答

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
這很簡(jiǎn)單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貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊
您在尋找這樣的東西嗎?
運(yùn)行下面的代碼片段:
//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貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
建議切換父級(jí)的樣式
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>
添加回答
舉報(bào)