3 回答

TA貢獻1829條經驗 獲得超7個贊
嘗試這個
它可以處理加載時選中復選框的情況
第一個用于一個復選框,第二個用于多個復選框。
const toggle = function() {
const chk = document.querySelector(".wfob_checkbox");
chk.closest(".wfob_wrapper").classList.toggle("hide", chk.checked)
};
window.addEventListener("load",function() {
document.querySelector(".wfob_checkbox").addEventListener("click",toggle)
toggle()
});
.hide { display:none; }
<div id="wfob_wrap" class="wfob_wrap_start">
<div class="wfob_wrapper" data-wfob-id="72396">
<div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
<div class="wfob_outer">
<div class="wfob_Box">
<div class="wfob_bgBox_table no_table">
<div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
<div class="wfob_order_wrap wfob_content_bottom_wrap">
<div class="wfob_bgBox_table_box">
<div class="wfob_bgBox_cell wfob_img_box">
<div class="wfob_checkbox_input_wrap">
<span class="wfob_bump_checkbox">
<input type="checkbox" class="wfob_checkbox wfob_bump_product">
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
如果有更多集合,則不能使用 ID
window.addEventListener("load", function() {
document.getElementById("container").addEventListener("click", function(e) {
const tgt = e.target;
if (tgt.classList.contains("wfob_checkbox")) {
tgt.closest(".wfob_wrap_start").querySelector(".wfob_wrapper").classList.toggle("hide", tgt.checked)
}
});
// loading
[...document.querySelectorAll(".wfob_checkbox:checked")].forEach(tgt =>
tgt.closest(".wfob_wrap_start").querySelector(".wfob_wrapper").classList.toggle("hide", tgt.checked)
)
});
.hide {
display: none;
}
<div id="container">
<div class="wfob_wrap_start">
<div class="wfob_wrapper" data-wfob-id="72396">
<div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
<div class="wfob_outer">
<div class="wfob_Box">
<div class="wfob_bgBox_table no_table">
<div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
<div class="wfob_order_wrap wfob_content_bottom_wrap">
<div class="wfob_bgBox_table_box">
<div class="wfob_bgBox_cell wfob_img_box">
<div class="wfob_checkbox_input_wrap">
<span class="wfob_bump_checkbox">
<input type="checkbox" class="wfob_checkbox wfob_bump_product">
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="wfob_wrap_start">
<div class="wfob_wrapper" data-wfob-id="72396">
<div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
<div class="wfob_outer">
<div class="wfob_Box">
<div class="wfob_bgBox_table no_table">
<div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
<div class="wfob_order_wrap wfob_content_bottom_wrap">
<div class="wfob_bgBox_table_box">
<div class="wfob_bgBox_cell wfob_img_box">
<div class="wfob_checkbox_input_wrap">
<span class="wfob_bump_checkbox">
<input type="checkbox" class="wfob_checkbox wfob_bump_product" checked>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

TA貢獻1877條經驗 獲得超6個贊
我給出了javascript和jquery的兩種解決方案。有必要嗎?
jquery版本:
$('.wfob_checkbox.wfob_bump_product').click(function(){
if($(this).is(':checked')) {
$(this).closest('#wfob_wrap .wfob_wrapper').css('display', 'none');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="wfob_wrap" class="wfob_wrap_start">
<div class="wfob_wrapper" data-wfob-id="72396">
<div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
<div class="wfob_outer">
<div class="wfob_Box">
<div class="wfob_bgBox_table no_table">
<div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
<div class="wfob_order_wrap wfob_content_bottom_wrap">
<div class="wfob_bgBox_table_box">
<div class="wfob_bgBox_cell wfob_img_box">
<div class="wfob_checkbox_input_wrap">
<span class="wfob_bump_checkbox">
<input type="checkbox" class="wfob_checkbox wfob_bump_product">
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
JavaScript 版本:
let checkbox = document.querySelector('.wfob_checkbox.wfob_bump_product');
checkbox.onclick = function() {
if (this.checked) {
this.closest('.wfob_wrapper').style.display = 'none';
}
}
<div id="wfob_wrap" class="wfob_wrap_start">
<div class="wfob_wrapper" data-wfob-id="72396">
<div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
<div class="wfob_outer">
<div class="wfob_Box">
<div class="wfob_bgBox_table no_table">
<div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
<div class="wfob_order_wrap wfob_content_bottom_wrap">
<div class="wfob_bgBox_table_box">
<div class="wfob_bgBox_cell wfob_img_box">
<div class="wfob_checkbox_input_wrap">
<span class="wfob_bump_checkbox">
<input type="checkbox" class="wfob_checkbox wfob_bump_product">
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

TA貢獻1780條經驗 獲得超1個贊
你可以使用這樣的東西:
document.querySelector('[type=checkbox]').addEventListener('change', e => {
? e.target.closest('#wfob_wrap').firstElementChild.style.display = 'none';
});
<div id="wfob_wrap" class="wfob_wrap_start">
? <div class="wfob_wrapper" data-wfob-id="72396">
? ? <div class="wfob_bump wfob_clear" data-product-key="wfob_5fb3c4fc409e4" data-wfob-id="72396">
? ? ? <div class="wfob_outer">
? ? ? ? <div class="wfob_Box">
? ? ? ? ? <div class="wfob_bgBox_table no_table">
? ? ? ? ? ? <div class="wfob_bgBox_tablecell no_table_cell wfob_check_container">
? ? ? ? ? ? ? <div class="wfob_order_wrap wfob_content_bottom_wrap">
? ? ? ? ? ? ? ? <div class="wfob_bgBox_table_box">
? ? ? ? ? ? ? ? ? <div class="wfob_bgBox_cell wfob_img_box">
? ? ? ? ? ? ? ? ? ? <div class="wfob_checkbox_input_wrap">
? ? ? ? ? ? ? ? ? ? ? <span class="wfob_bump_checkbox">
? ? ? ? ? ? ? ? ? ? ? ? <input type="checkbox" class="wfob_checkbox wfob_bump_product" checked="">
? ? ? ? ? ? ? ? ? ? ? </span>
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? ? </div>
? ? ? ? </div>
? ? ? </div>
? ? </div>
? </div>
</div>
我使用.closest(..)來獲取#wfob_wrap
元素,并使用 .firstElementChild來獲取該元素的第一個子元素。
在這種特定情況下,由于您要查找的元素有一個 id,因此您不需要.closest(..)
,您可以直接使用 來獲取該元素document.getElementById('wfob_wrap')
。
添加回答
舉報