简体   繁体   English

根据与网址中的哈希值匹配的锚点Href属性显示/隐藏Divs

[英]Show/Hide Divs based on Anchor Href attr that matches the Hash in the URL

I have found a lot of info on URL's with Hash, but nothing to solve this. 我发现了很多有关Hash的URL的信息,但是没有任何解决方案。 I have divs that are hidden then shown when you click it's parent. 我有隐藏的div,然后在单击它的父项时显示。 What I want to add in is if you come in with a #Hash tag that matches the anochor's href attr - trigger it was clicked. 我要添加的是如果您输入的#Hash标记与anochor的href属性相匹配-触发它被单击。

Code I have now: 我现在拥有的代码:

$('.speaker_container').on('click','.speaker_img',function(){ $(this).siblings('.speaker-bios').toggle(150);});


 <section id="speakers">
<div class="speaker_container clearfix">
    <div class="speaker_img">
        <a class="speaker_img" href="#speaker1"><img>Text</a>
    </div>
    <h3>Name</h3>
    <p class="clearfix">Text</p>
    <div class="speaker-bios" style="display: none;">
        <p>Hidden text</p>
    </div>
</div>
<div class="speaker_container clearfix">
    <div class="speaker_img">
        <a class="speaker_img" href="#speaker2"><img>Text</a>
    </div>
    <h3>Name</h3>
    <p class="clearfix">Text</p>
    <div class="speaker-bios" style="display: none;">
        <p>Hidden text</p>
    </div>
</div>
and so on....

` `

This should do the trick. 这应该可以解决问题。

$(function(){
   var hash = window.location.hash;
   if(hash != 'undefined'){
      $('a[href='+hash+']').parents('div.speaker_container').trigger('click');

   }
});

How about (if JQuery) 怎么样(如果是JQuery)

var hash = window.location.hash;
$(hash).show();

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

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