简体   繁体   English

从链接打开可折叠部分

[英]Open Collapsible Section From Link

I've made a page that has expandable/collapsible sections but I can't figure out how to get them to open from a navigation link.我制作了一个具有可展开/可折叠部分的页面,但我不知道如何从导航链接打开它们。 The expandable/collapsible sections expand as I want them to when they are clicked on, and my nav links take me to the correct section, however, I'd also like the nav links to expand the appropriate section in addition to taking me to that part of the page.可展开/可折叠部分在点击时会按照我希望的方式展开,并且我的导航链接会将我带到正确的部分,但是,除了将我带到那个部分之外,我还希望导航链接可以展开适当的部分页面的一部分。 I'm a bit of a novice so any help will be appreciated.我有点新手,所以任何帮助将不胜感激。

Here is my HTML & CSS & jQuery:这是我的 HTML & CSS & jQuery:

 var coll = document.getElementsByClassName("collapsible"); var i; for (i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = this.nextElementSibling; if (content.style.display === "block") { content.style.display = "none"; } else { content.style.display = "block"; } }); }
 .collapsible { background-color: #eee; font: 15px/30px "raleway-heavy", sans-serif; text-shadow: 0 1px 4px rgba(0, 0, 0, 0.6); text-transform: uppercase; letter-spacing: 2.5px; margin-bottom: 30px; margin-top: 18px; left: 3px; color: #4c4c4c; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: left; outline: none; font-size: 15px; } .active, .collapsible:hover { background-color: #fdc501; color: #ffffff; } .collapsed-content { padding: 0 18px; display: none; overflow: hidden; margin-bottom:30px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav id="nav-wrap"> <ul id="nav" class="nav"> <li><a class="smoothscroll" href="#link1">Link 1</a></li> <li><a class="smoothscroll" href="#link2">Link 2</a></li> </ul> <!-- end #nav --> </nav> <!-- end #nav-wrap --> <!-- link1 --> <section id="link1"> <button type="button" class="collapsible">Link 1 Title<span style="color:#e4b101;">.</span></button> <div class="collapsed-content"> <hr /> <p>Link 1 content.</p> </div> </section> <!-- end link1 --> <!-- link2 --> <section id="link2"> <button type="button" class="collapsible">Link 2 Title<span style="color:#e4b101;">.</span></button> <div class="collapsed-content"> <hr /> <p>Link 2 content.</p> </div> </section> <!-- end link2 --> <br /><br /><br /><br /><br /><br />

Here is a jsfiddle of my code:这是我的代码的 jsfiddle:

https://jsfiddle.net/AceCrusher/4dporn7a/2/ https://jsfiddle.net/AceCrusher/4dporn7a/2/

Consider using data-* attributes on links考虑在链接上使用 data-* 属性

<li><a class="smoothscroll" href="#link1" data-target="#link1">Link 1</a></li>
<li><a class="smoothscroll" href="#link2" data-target="#link2">Link 2</a></li>

You can then add click event listeners to all of the links and when the user clicks the link, expand the content.然后,您可以向所有链接添加单击事件侦听器,并在用户单击链接时展开内容。 You can use something like this:你可以使用这样的东西:

const links = document.querySelectorAll('.smoothscroll')

links.forEach(link => {
    link.addEventListener('click', () => {
        const section = document.querySelector(link.dataset.target)
        const content = section.querySelector('.collapsed-content')
        if (content.style.display === "block") {
          content.style.display = "none";
        } else {
          content.style.display = "block";
        }
    })
})

One option is to add an onClick event to the the navigation link that runs the code for expanding the appropriate section.一种选择是将 onClick 事件添加到运行代码以展开相应部分的导航链接。

Your current event listener handles this relative to the "collapsible" element that the handler is attached to.您当前的事件侦听器相对于处理程序附加到的“可折叠”元素来处理此问题。 If you wanted to reuse it for both the button click and link click, you would have to rework it to accept a particular collapsible as argument.如果你想在按钮点击和链接点击中重用它,你必须重新设计它以接受特定的可折叠作为参数。

Here you go !干得好 ! I added data-link element to your links and then send filtering to get good elements.我在你的链接中添加了数据链接元素,然后发送过滤以获得好的元素。

 var coll = document.getElementsByClassName("collapsible"); var navLink = document.getElementsByClassName("smoothscroll"); var i; for (i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = this.nextElementSibling; if (content.style.display === "block") { content.style.display = "none"; } else { content.style.display = "block"; } }); } for (i = 0; i < navLink.length; i++) { navLink[i].addEventListener("click", function() { var link = this.dataset.link; var sectionId = document.getElementById(link); var divToOpen = sectionId.querySelector(".collapsible"); divToOpen.classList.toggle("active"); var content = divToOpen.nextElementSibling; if (content.style.display === "block") { content.style.display = "none"; } else { content.style.display = "block"; } }); }
 .collapsible { background-color: #eee; font: 15px/30px "raleway-heavy", sans-serif; text-shadow: 0 1px 4px rgba(0, 0, 0, 0.6); text-transform: uppercase; letter-spacing: 2.5px; margin-bottom: 30px; margin-top: 18px; left: 3px; color: #4c4c4c; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: leftcollapsible outline: none; font-size: 15px; } .active, .collapsible:hover { background-color: #fdc501; color: #ffffff; } .collapsed-content { padding: 0 18px; display: none; overflow: hidden; margin-bottom:30px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav id="nav-wrap"> <ul id="nav" class="nav"> <li><a class="smoothscroll" data-link="link1" href="#link1">Link 1</a></li> <li><a class="smoothscroll" data-link="link2" href="#link2">Link 2</a></li> </ul> <!-- end #nav --> </nav> <!-- end #nav-wrap --> <!-- link1 --> <section id="link1"> <button type="button" class="collapsible">Link 1 Title<span style="color:#e4b101;">.</span></button> <div class="collapsed-content"> <hr /> <p>Link 1 content.</p> </div> </section> <!-- end link1 --> <!-- link2 --> <section id="link2"> <button type="button" class="collapsible">Link 2 Title<span style="color:#e4b101;">.</span></button> <div class="collapsed-content"> <hr /> <p>Link 2 content.</p> </div> </section> <!-- end link2 --> <br /><br /><br /><br /><br /><br />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM