简体   繁体   English

使用ID和哈希显示/隐藏div

[英]Show/hide divs using IDs and hashes

I'm looking to create a simple show/hide div set up using IDs and hash values. 我正在寻找使用ID和哈希值创建一个简单的show / hide div设置。 If you click the link, which has a hash value, it shows the div with the same ID as the hash. 如果单击具有哈希值的链接,它将显示与哈希具有相同ID的div。 I know there's a few tools out there, but I'd prefer to do this using jQuery/JS if possible. 我知道那里有一些工具,但是如果可能的话,我更喜欢使用jQuery / JS。

One thing to note is that all the hashes, and IDs are pulled in dynamically from the CMS based on the page title, then sanitises it — so I can't hard-code any IDs or hashes in the jQuery. 需要注意的一件事是,所有哈希和ID都是根据页面标题从CMS动态提取的,然后对其进行清理-因此,我无法在jQuery中对任何ID或哈希进行硬编码。

Here's my set up: 这是我的设置:

<div class="menu">
  <ul>
    <li><a href="#aquatic-health">Aquatic Health</a></li> 
    <li><a href="#environment">Environment</a></li> 
  </ul>
</div>

<div id="aquatic-health">
  <p>Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.&nbsp;Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.&nbsp;Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.</p> <p>Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.</p>
</div>
<div id="environment">
  <p>Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.&nbsp;Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.&nbsp;Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.</p> <p>Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.</p>
</div>

Many many thanks in advance, R 非常感谢,R

You can try this : 您可以尝试以下方法:

$('.menu a').on('click', function(){
    $( $(this).attr('href') ).toggle(); // prop() can be used
});

fiddle : 小提琴:

http://jsfiddle.net/FsCHV/ http://jsfiddle.net/FsCHV/

Something like this (if you can add a class to each content div when they are generated) : 这样的事情(如果您可以在生成每个内容div时将其添加一个类):

EDIT => display the first div by default 编辑=>默认显示第一个div

http://jsfiddle.net/eNTtN/3/ http://jsfiddle.net/eNTtN/3/

HTML : HTML:

<div class="menu">


<ul>
    <li><a href="#aquatic-health">Aquatic Health</a></li> 
    <li><a href="#environment">Environment</a></li> 
  </ul>
</div>

<div class="hidden" id="aquatic-health">
  <p>Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.&nbsp;Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.&nbsp;Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.</p> <p>Lorem ipsum dolor sit amet, vel vide elaboraret persequeris at, clita vitae eirmod in eos, eros nostrud apeirian ea per.</p>
</div>
<div class="hidden" id="environment">
  <p>Blabla</p>
</div>

CSS 的CSS

.hidden{
    display: none;
}

JS JS

$(document).ready(function(){
    $(".menu li a").click(function(){
        $(".hidden").hide();
        $($(this).attr("href")).show();
    });
    /* Show the first div by default */
    $(".hidden:first").show();
});

Try this. 尝试这个。

$(.menu li a).click(function(){
var hrefValue = $(this).attr("href");
$(hrefValue).toggle();
});
$(function(){
  $('.menu').find('a').on('click', function(){
    var dest = $(this).attr('href');
    $(dest).slideToggle();
  });
});

This will do.. 这样就可以了

$(document).ready(function() {

    $('.menu a').click(function() {
        var id = $(this).attr('href');
        $(id).css('display', 'block');
    });

});

Or you can use a toggle 或者您可以使用切换键

$(document).ready(function() {

        $('.menu a').click(function() {
            var id = $(this).attr('href');
            $(id).toggle();
        });

    });

http://jsfiddle.net/FZTmH/ http://jsfiddle.net/FZTmH/

You can make yourself a close button. 您可以使自己成为关闭按钮。

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

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