【发布时间】:2021-08-05 12:48:01
【问题描述】:
对不起,如果这令人困惑。我有一组包含标题的六个 div。我希望这样当您单击 div 时,会在显示信息的标题下打开一个新 div。当用户点击下一个标题时,我希望它关闭打开的 div,打开新的 div,然后滚动到新打开的 div 的标题。
到目前为止,我可以让它做所有事情,除了滚动到我刚刚点击的 div。我已经尝试了很多在这里找到的不同代码,但是我对 jquery 的了解还不够,无法弄清楚。任何帮助将不胜感激。
您可以在此处查看页面 - http://justinj16.sg-host.com/services/
现在只有前两个标签被设置为打开(如果我不能让它工作,我不想再花时间了)。
这是我使用的代码 -
<script>jQuery(document).ready(function($) {
//Hide all content to be revealed
$('.reveal_content').hide();
//Create the click event for each reveal button
$('.reveal_button').click(function (e) {
//Prevent the default button action (i.e. redirecting to a url)
e.preventDefault();
//Get the parent section for the button clicked
$parent_section = $(this).closest('.reveal_section');
//Remove the opened class from all other buttons
$('.reveal_button').not(this).removeClass('opened');
//Close all content in other sections
$('.reveal_section').not($parent_section).find('.reveal_content').fadeOut('slow');
//Hide/Reveal the content in the same section as the button clicked
$(this).addClass('opened');
$parent_section.find('.reveal_content').each(function() {
$(this).slideToggle();
}
)
});
}
)</script>
【问题讨论】:
-
你真的想要一个 jquery 解决方案还是可以使用 vanilla JS 解决方案?
-
你能不能在这里在一个 sn-p 上设置一个例子,只有那部分让你感到困扰。没有多少人会乐意挖掘您的所有代码来帮助您。
标签: javascript html jquery css