简体   繁体   English

如何从另一个可折叠项目[js]中的链接展开可折叠项目?

[英]How to expand a collapsible item from link within another collapsible item [js]?

I'm making a list of collapsible items that can make calls to itself to scroll to and expand other items on the fly. 我正在制作一个可折叠项目列表,可以调用自己来滚动和动态扩展其他项目。 Shooting for this... 为此射击......

Before selecting the hyperlink. 在选择超链接之前。 在此输入图像描述

After selecting. 选择后。 在此输入图像描述

How can I get the First collapsed item to expand when the link to it in the Third item's paragraph is selected? 当选择第三个项目段落中的链接时,如何将第一个折叠项目展开?

What I got : If the example above had more collapsed items, then the code below would scroll the webpage to the desired collapsible item (half the solution). 我得到了 :如果上面的示例有更多折叠项目,那么下面的代码会将网页滚动到所需的可折叠项目(解决方案的一半)。

<!DOCTYPE html>
<html>
    <head>
      <!--Import materialize.css-->
      <link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>
      <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    </head>
    <body>
    <div>
        <ul class="collapsible" data-collapsible="accordion">
            <li>
              <div class="collapsible-header"><i class="mdi-navigation-chevron-right"></i><a name="987"/>First</a></div>
              <div class="collapsible-body"><p>Hello StackOverflow! SO's da' bomb diggidy!</p></div>
            </li>
            <li>
              <div class="collapsible-header"><i class="mdi-navigation-chevron-right"></i>Second</div>
              <div class="collapsible-body"><p>Why is the person who invests your money called a broker?</p></div>
            </li>
            <li>
              <div class="collapsible-header"><i class="mdi-navigation-chevron-right"></i>Third</div>
              <div class="collapsible-body"><p>I'd like to <a href="#987">open the First collapsible element</a> in this list.</p></div>
            </li>
        </ul>
    </div>
      <!--Import jQuery before materialize.js-->
      <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
      <script type="text/javascript" src="js/materialize.min.js"></script>
    </body>
  </html>

I would just trigger click event on first .collapsible-header item, with slightly changed html code for the anchor: 我只会在第一个.collapsible-header项目上触发click事件,稍微更改了锚点的html代码:

 $('[data-click]').on('click', function (e) { $( $(this).data('click') ).trigger('click'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.95.3/js/materialize.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.95.3/css/materialize.min.css" rel="stylesheet"/> <div> <ul class="collapsible" data-collapsible="accordion"> <li> <div class="collapsible-header"><i class="mdi-navigation-chevron-right"></i>First</div> <div class="collapsible-body"> <p>Hello StackOverflow! SO's da' bomb diggidy!</p> </div> </li> <li> <div class="collapsible-header"><i class="mdi-navigation-chevron-right"></i>Second</div> <div class="collapsible-body"> <p>Why is the person who invests your money called a broker?</p> </div> </li> <li> <div class="collapsible-header"><i class="mdi-navigation-chevron-right"></i>Third</div> <div class="collapsible-body"> <p>I'd like to <a href="#" data-click=".collapsible .collapsible-header:first">open the First collapsible element</a> in this list.</p> </div> </li> </ul> </div> 

Same code is also on Fiddle . 同样的代码也在小提琴上

If you wish to target n-th element, than you can use jQuery :eq() selector (zero based). 如果你希望定位第n个元素,那么你可以使用jQuery:eq()选择器 (基于零)。 For example, to target 3rd item you would use '.collapsible-header:eq(2)' selector. 例如,要定位第3项,您将使用'.collapsible-header:eq(2)'选择器。


If you care about the SEO (and you should), than your links should have correct href . 如果你关心SEO (你应该),你的链接应该有正确的href In this case add unique IDs to .collapsible_header elements and use slightly different script to exploit it: 在这种情况下,为.collapsible_header元素添加唯一ID,并使用略有不同的脚本来利用它:

$('[data-click]').on('click', function (e) {
    $( $(this).attr('href') ).trigger('click');
});

where the target item is: 目标项目在哪里:

<div id="about_stackoverflow" class="collapsible-header">

and the valid local link is: 有效的本地链接是:

<a href="#about_stackoverflow" title="Click to open first item" data-click="true">open the First collapsible element</a>

You can see it working on this Fiddle . 你可以看到它在这个小提琴上工作。 (The last anchor may be anywhere on the same page) (最后一个锚点可能位于同一页面的任何位置)

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

相关问题 如何限制可折叠项保持展开状态,除非我单击其他可折叠项进行展开,而不单击可折叠项本身即可 - How do I restrict a collapsible item to stay expanded unless i click the other collapsible items to expand but not the collapsible item itself 另一个可折叠部分中的可折叠部分不会充分展开 - Collapsible section within another collapsible section won't expand enough 如何维护可折叠项目 position? - How can I maintain collapsible item position? 如何从外部链接打开jQuery Collapsible.js - How to open jQuery Collapsible.js from external link 在js中以编程方式展开可折叠集 - Expand a collapsible set programatically in js jQuery mobile:如何在可折叠项目的标题中添加一个复选框 - jQuery mobile: how to add a checkbox in header of collapsible item 如何防止用户选择下一个可折叠项目,直到之前的响应被填满? - How to prevent user from selecting next collapsible item until previous responses are filled? 如何在jquery mobile的中心制作可折叠项目的标题 - How to make the Heading of collapsible item at the center in jquery mobile 如何在反应中默认打开所有可折叠项目? - How to make all the collapsible item open by default in react? Bootstrap 4:如何显示一个可折叠项目并隐藏其他项目? - Bootstrap 4: How to show one collapsible item and hide others?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM