我正在寻找一种方法来根据点击的内容切换类。举个例子,我有这个:
<div id="element_1"> <!-- notice unique id attached to the end -->
<button>My Button</button>
<div class="hidden_wrapper">
<span>Something</span>
<span>Something else</span>
</div>
</div>
据我了解,我可以执行以下操作来触发该唯一 ID(未经测试):
$(document).on("click", "[id^=element] button", function() { $('[id^=element] hidden_wrapper').toggleClass('some_class'); });
#element_1 .hidden_wrapper
如果单击了我之外的任何内容,我想要在该唯一 ID 下切换该类(实际上将删除该类)
有人可以在这里阐明一下吗?