簡體   English   中英

根據與網址中的哈希值匹配的錨點Href屬性顯示/隱藏Divs

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

我發現了很多有關Hash的URL的信息,但是沒有任何解決方案。 我有隱藏的div,然后在單擊它的父項時顯示。 我要添加的是如果您輸入的#Hash標記與anochor的href屬性相匹配-觸發它被單擊。

我現在擁有的代碼:

$('.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....

`

這應該可以解決問題。

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

   }
});

怎么樣(如果是JQuery)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM