gpt4 book ai didi

jquery - SlideToggle div 不起作用

转载 作者:行者123 更新时间:2023-12-01 04:03:28 25 4
gpt4 key购买 nike

我有这个代码:

$('.close-button').click(function() {
$(this).closest('#hidden').slideToggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="bloc">
<h2>Click on that link to display the content <a href="#" class="close-button">Click here</a></h2>
<div id="hidden">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus augue ante, fringilla ac sodales et, vehicula pellentesque ante. Integer id mattis erat, nec tincidunt nisi. </p>
</div>
</div>

<div class="bloc">
<h2>Click on that link to display the content <a href="#" "close-button">Click here</a></h2>
<div id="hidden">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus augue ante, fringilla ac sodales et, vehicula pellentesque ante. Integer id mattis erat, nec tincidunt nisi. </p>
</div>
</div>

单击链接时,将显示 div #hidden

我尝试在 Jquery 中执行此操作,但它不起作用。

感谢您的帮助!

最佳答案

不要使用重复的id,而是使用class

.closest 仅通过 parent 查找,而不是通过 sibling 查找这些 parent 。

$('.close-button').click(function() {
$(this).parent().next().slideToggle();
});
.hidden{
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="bloc">
<h2>Click on that link to display the content <a href="#" class="close-button">Click here</a></h2>
<div class="hidden">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus augue ante, fringilla ac sodales et, vehicula pellentesque ante. Integer id mattis erat, nec tincidunt nisi. </p>
</div>
</div>

<div class="bloc">
<h2>Click on that link to display the content <a href="#" class="close-button">Click here</a></h2>
<div class="hidden">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus augue ante, fringilla ac sodales et, vehicula pellentesque ante. Integer id mattis erat, nec tincidunt nisi. </p>
</div>
</div>

关于jquery - SlideToggle div 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35287519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com