Javascript 基于与URL中的哈希匹配的锚Href attr显示/隐藏div

Javascript 基于与URL中的哈希匹配的锚Href attr显示/隐藏div,javascript,jquery,show-hide,hashtag,Javascript,Jquery,Show Hide,Hashtag,我在URL上找到了很多关于哈希的信息,但是没有什么可以解决这个问题。我有隐藏的div,当你点击它的父对象时就会显示出来。我想添加的是,如果您输入的哈希标记与Anocho的href attr-trigger匹配,则单击它 我现在有: $('.speaker_container').on('click','.speaker_img',function(){ $(this).siblings('.speaker-bios').toggle(150);}); <section id="spe

我在URL上找到了很多关于哈希的信息,但是没有什么可以解决这个问题。我有隐藏的div,当你点击它的父对象时就会显示出来。我想添加的是,如果您输入的哈希标记与Anocho的href attr-trigger匹配,则单击它

我现在有:

$('.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....
`如果JQuery

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

这应该能奏效

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

   }
});

也许您可以提供一个JSFIDLE,使其更易于贡献。这将不起作用,因为哈希开始于$hash将尝试按id选择元素,但它应该由href选择。Had必须将div.speaker_容器切换到.speaker_img$函数{var hash=window.location.hash;ifhash!='undefined'{$'a[href='+hash+']'。parents.speaker_img'。触发器'click';};