简体   繁体   English

通过jQuery切换Div的开和关

[英]Toggling the Div's on and off through jQuery

Stackoverflowers! Stackoverflowers!

Over the last few post's you have all helped me get extremely far and I am now facing another problem if you are able to help me, I also think that the last times I have not explained very well, so here it goes. 在过去的几篇文章中,您都帮助我取得了长足的进步,如果您能帮助我,我现在面临另一个问题,我还认为上次我的讲解还不是很好,因此请继续。

I have 3 id divs, named: white, v2black, v3black. 我有3个ID div,分别为:white,v2black,v3black。 My aim is to have a hyperlink nav that toggles each one of the Div's on and Off; 我的目标是拥有一个超链接导航,该导航可切换Div的每个打开和关闭; however, when one is clicked, it must hide the current active div so two are not visible at the same time. 但是,当单击一个时,它必须隐藏当前活动的div,这样就不会同时显示两个。

Just incase, the link for the source and the live site is: www.steveatattooartist.com 以防万一,来源和实时网站的链接是:www.steveatattooartist.com

I have tried enough and this is where I am at the moment: 我已经尽力了,这就是我现在所处的位置:

My jQuery: 我的jQuery:

<script>
 jQuery(function() {
  $('.toggle').on('click', function() {
    var id = $(this).attr('data-for');
    $('#' + id).toggle();
  });
});
</script>

My Html Structure: 我的HTML结构:

<div class="altstevenav" style="display:none">
<div class="stevenav">
<ul class="navigation">
<li><a href="#firstpagename" id="firstpagename"><?php echo the_field('first_page_name'); ?></a></li>
<li><a href="#secondpagename" id="secondpagename"><?php echo the_field('second_page_name'); ?></a></li>
<li><a href="#thirdpagename" id="thirdpagename"><?php echo the_field('third_page_name'); ?></a></li>
</ul>
</div>
</div>

</div> <!--MAIN CLOSING DIV-->
</div> <!--CONTENT CLOSING DIV-->

<div class="delstevenav">
<div class="stevenav">
<ul class="navigation">
<li><a href="#firstpagename" id="firstpagename"><?php echo the_field('first_page_name'); ?></a></li>
<li><a href="#secondpagename" id="secondpagename"><?php echo the_field('second_page_name'); ?></a></li>
<li><a href="#thirdpagename" id="thirdpagename"><?php echo the_field('third_page_name'); ?></a></li>
</ul>
</div>
</div>
<!--ALL DIVS ARE CLOSE HERE-->

<!--Divisional Panels-->
<div id="white  class="toggle" data-for="white">
<div style="width:968px; margin: 0 auto;">
<div class="panel" style="color:#000000;">
<div style="width:450px; float:left;">
<img src="<?php echo the_field('image_one_of_biography'); ?>" width="425" alt="Biography Image" />
<br /><br />
<img src="<?php echo the_field('image_two_of_biography'); ?>" width="425" alt="Biography Image" />
<br /><br />
<img src="<?php echo the_field('image_three_of_biography'); ?>" width="425" alt="Biography Image" />
</div>
<div style="width:450px; float:left; text-align:left;">
<?php echo the_field('biography_content'); ?>
</div>
</div>
</div>

<div id="v2black" class="toggle" data-for="v2black">
<div style="width:968px; height:500px; margin: 0 auto;">

</div>
</div>

<div id="v3black"  class="toggle" data-for="v3black">
<div style="width:968px; height:500px; margin: 0 auto;">

</div></div>

</div>

Really hope someone could help me because you guys have seriously helped me get really far in this project and I appreciate it, A LOT! 真的希望有人能帮助我,因为你们已经认真地帮助我在该项目中取得了长足的进步,我对此表示赞赏,很多!

Your div's need to have a common attribute (eg class), so you can hide them all first, and then show the active div. 您的div需要具有一个公共属性(例如class),因此您可以先将它们全部隐藏,然后再显示活动的div。 The toggle class is common to all of them, so: toggle类对所有它们都是通用的,因此:

$('.toggle').on('click', function() {
    $(".toggle").hide();
    var id = $(this).attr('data-for');
    $('#' + id).show();
});

UPDATE: 更新:

Using the console in my browser, I changed the links to look like this: 使用浏览器中的控制台,我将链接更改为如下所示:

<div class="altstevenav" style="display: none; ">
    <div class="stevenav">
        <ul class="navigation">
            <li>
                <a href="#firstpagename" id="firstpagename" data-for="white">Steve A</a>
            </li>
            <li>
                <a href="#secondpagename" id="secondpagename" data-for="v2black">Tattoos</a>
            </li>
            <li>
                <a href="#thirdpagename" id="thirdpagename" data-for="v3black">Sketchbook</a>
            </li>
        </ul>
    </div>
</div>

<div class="stevenav">
    <ul class="navigation">
        <li>
            <a href="#firstpagename" id="firstpagename2" data-for="white">Steve A</a>
        </li>
        <li>
            <a href="#secondpagename" id="secondpagename2" data-for="v2black">Tattoos</a>
        </li>
        <li>
            <a href="#thirdpagename" id="thirdpagename2" data-for="v3black">Sketchbook</a>
        </li>
    </ul>
</div>

then I re-assigned the click handlers thusly: 然后我因此重新分配了点击处理程序:

$('ul.navigation a[data-for]').click(function (e) {
    var targetDiv = $(this).attr('data-for');
    $('div.common').hide();
    $('div#' + targetDiv).show();
    e.preventDefault();
    return false;
});

You will probably also want to do: 您可能还想做:

$('div.common').hide();
$('div#white').show();

somewhere in $(document).ready() to have only the white div showing initially. $(document).ready()某个位置最初只显示white div。

This seemed to have the desired effect. 这似乎具有预期的效果。 Try these changes and let us know if that worked for you. 尝试这些更改,并告诉我们是否对您有用。

-- previous answer below -- -下面的先前答案-

A couple of suggestions: 一些建议:

  1. The line <div id="white class="toggle" data-for="white"> should probably be <div id="white" class="toggle" data-for="white"> instead. <div id="white class="toggle" data-for="white"> ,行<div id="white class="toggle" data-for="white">应该改为<div id="white" class="toggle" data-for="white">
  2. The only real difference between div.altstevenav and div.delstevenav is the containing element. div.altstevenavdiv.delstevenav之间的唯一真正区别是包含元素。 You should either give the links in one different id attributes or consider not using id attributes at all. 您应该在一个不同的id属性中提供链接,或者考虑完全不使用id属性。
  3. The v2black and v3black divs are both children of the white div. v2blackv3black div都是white div的子级。 You should make them siblings of the white div, either by moving them out of the white div, or by using the current div (with id="white" ) as a container div (with no id attribute) and making a new child div of the container with id="white" instead. 您应通过将它们移出white div或使用当前div(具有id="white" )作为容器div(不具有id属性)并使其成为新的子div来使它们成为white div的兄弟。容器的id="white"代替。
  4. For the links that you want to use to trigger the div visibility-toggling action, you should move the data-for attributes to the those links instead of on the target divs. 对于要用于触发div可见性切换操作的链接,应将data-for属性移到那些链接上,而不是在目标div上。

Example of links (Point 2): 链接示例(第2点):

<div class="altstevenav" style="display:none">
    <div class="stevenav">
        <ul class="navigation">
            <li>
                <a href="#firstpagename" id="altfirstpagename">Steve A</a>
            </li>
            <li>
                <a href="#secondpagename" id="altsecondpagename">Tattoos</a>
            </li>
            <li>
                <a href="#thirdpagename" id="altthirdpagename">Sketchbook</a>
            </li>
        </ul>
    </div>
</div>
</div>
<!--MAIN CLOSING DIV-->
</div>
<!--CONTENT CLOSING DIV-->
<div class="delstevenav">
    <div class="stevenav">
        <ul class="navigation">
            <li>
                <a href="#firstpagename" id="delfirstpagename">Steve A</a>
            </li>
            <li>
                <a href="#secondpagename" id="delsecondpagename">Tattoos</a>
            </li>
            <li>
                <a href="#thirdpagename" id="delthirdpagename">Sketchbook</a>
            </li>
        </ul>
    </div>
</div>

or 要么

 <div class="altstevenav" style="display:none">
    <div class="stevenav">
        <ul class="navigation">
            <li>
                <a href="#firstpagename">Steve A</a>
            </li>
            <li>
                <a href="#secondpagename">Tattoos</a>
            </li>
            <li>
                <a href="#thirdpagename">Sketchbook</a>
            </li>
        </ul>
    </div>
</div>
</div>
<!--MAIN CLOSING DIV-->
</div>
<!--CONTENT CLOSING DIV-->
<div class="delstevenav">
    <div class="stevenav">
        <ul class="navigation">
            <li>
                <a href="#firstpagename">Steve A</a>
            </li>
            <li>
                <a href="#secondpagename">Tattoos</a>
            </li>
            <li>
                <a href="#thirdpagename">Sketchbook</a>
            </li>
        </ul>
    </div>
</div>

You can then use the follwing jQuery to get the job done (Point 4): 然后,您可以使用下面的jQuery完成工作(第4点):

$('a[data-for]').click(function (e) {
    var targetDiv = $(this).attr('data-for');
    $('div.toggle').hide();
    $(targetDiv).show();
    e.preventDefault();
    return false;
});

toggle to hide not current ones: 切换以隐藏不当前的:

$('.toggle').on('click', function() {
    $(".toggle").not('#' + $(this).data('for')).hide();
});

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

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