我需要在單擊「.toggler_btn」時顯示和隱藏div('.element_wrapper'),以便顯示上部內部的所有子div('.element_wrapper')一。另外,它必須在第二次點擊時隱藏它們。一切都應該以某種方式使用data-id和data-parent屬性來完成,這些屬性表示div的div id和父div的id和id。在點擊時顯示和隱藏Div-JS
的代碼:
<div id="wrapper">
<div class="element_wrapper" data-id="1" data-parent="" style="display:none">
<button class="toggler_btn" type="button" data-id="1" data-parent="" >+/-</button>
Main Office
<button class="add_btn" type="button">Add</button>
<button class="edit_btn" type="button">Edit</button>
<button class="delete_btn" type="button">Delete</button>
<div class="element_wrapper" data-id="3" data-parent="1" style="display:none">
<button class="toggler_btn" type="button" data-id="3" data-parent="1" >+/-</button>
Room 203
<button class="add_btn" type="button">Add</button>
<button class="edit_btn" type="button">Edit</button>
<button class="delete_btn" type="button">Delete</button>
<div class="element_wrapper" data-id="6" data-parent="3" style="display:none">
<button class="toggler_btn" type="button" data-id="6" data-parent="3" >+/-</button>
Table 2
<button class="add_btn" type="button">Add</button>
<button class="edit_btn" type="button">Edit</button>
<button class="delete_btn" type="button">Delete</button>
</div>
</div>
<div class="element_wrapper" data-id="4" data-parent="1" style="display:none">
<button class="toggler_btn" type="button" data-id="4" data-parent="1" >+/-</button>
Room 256
<button class="add_btn" type="button">Add</button>
<button class="edit_btn" type="button">Edit</button>
<button class="delete_btn" type="button">Delete</button>
<div class="element_wrapper" data-id="7" data-parent="4" style="display:none">
<button class="toggler_btn" type="button" data-id="7" data-parent="4" >+/-</button>
Table 3
<button class="add_btn" type="button">Add</button>
<button class="edit_btn" type="button">Edit</button>
<button class="delete_btn" type="button">Delete</button>
</div>
</div>
</div>
<div class="element_wrapper" data-id="2" data-parent="" style="display:none">
<button class="toggler_btn" type="button" data-id="2" data-parent="" >+/-</button>
Not So Main Office
<button class="add_btn" type="button">Add</button>
<button class="edit_btn" type="button">Edit</button>
<button class="delete_btn" type="button">Delete</button>
<div class="element_wrapper" data-id="5" data-parent="2" style="display:none">
<button class="toggler_btn" type="button" data-id="5" data-parent="2" >+/-</button>
Room 301
<button class="add_btn" type="button">Add</button>
<button class="edit_btn" type="button">Edit</button>
<button class="delete_btn" type="button">Delete</button>
<div class="element_wrapper" data-id="8" data-parent="5" style="display:none">
<button class="toggler_btn" type="button" data-id="8" data-parent="5" >+/-</button>
Table 13
<button class="add_btn" type="button">Add</button>
<button class="edit_btn" type="button">Edit</button>
<button class="delete_btn" type="button">Delete</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('div[data-parent=""]').show();
});
$(document).ready(function() {
$('.toggler_btn').on('click', function (event) {
var btn = $('.toggler_btn');
var elem = $('.element_wrapper');
if (btn.data('id') == elem.data('id')) {
elem.show();
}
})
});
</script>
你忘了提問了。顯示的代碼有什麼問題? 'data-parent'屬性似乎不是必須的:你不能僅僅使用'.closest()'或'.parents()'和'.find()'的組合來獲得點擊按鈕的包裝和關聯嵌套物品? – nnnnnn
@nnnnnn我認爲他們是必要的,因爲它的數據來自數據庫。或者試試你的方式,我會如何做到這一點。我是JS新手,所以我已經嘗試了很多,但仍然無法正常工作。 – SonOfAGun