简体   繁体   English

单击后显示隐藏的div

[英]display hidden div after click

I'm currently working on this website . 我目前正在这个网站上工作。

When I hover over a project in my index, a number appears. 当我将鼠标悬停在索引中的项目上时,将出现一个数字。 I am trying to make it so that number stays there if I click on the project. 我正在尝试使它保持数字,如果我单击该项目,则该数字将保持不变。 And then disappears again if I click on a different project. 如果我点击不同的项目,然后再次消失。

Right now my code looks something like this: 现在,我的代码如下所示:

$('.project').mouseover(function(){
    $(this).prev().show()
})  
$('.project').mouseout(function(){
    $(this).prev().hide()
})  
$('.project').click(function(){
    $(this).prev().show()
})

HTML: HTML:

            <!-- Project -->
                <div data-accordion>
            <!-- Number -->
                    <div class="number" id="n1">1</div>
            <!-- Title -->
                    <a class="project slide-link" id="p1" data-slide-id="1" data-control>Midi Matilda</a>

            <!-- Tags -->
                    <a class="tag t1">(Identity)</a>
                    <a class="tag t1">(Music)</a>

                        <div data-content>

                            <div class="info">This project is cool.</div>

                        </div>
                </div>

            <!-- Project -->
                <div data-accordion>
            <!-- Number -->
                    <a class="number" id="n2">2</a>
            <!-- Title -->
                    <a class="project slide-link" id="p2" data-slide-id="2" data-control>The Independent</a> 
            <!-- Tags -->
                    <a class="tag t2">(Poster)</a>

                        <div data-content>

                                <div class="info">This project is cooler.</div>

                        </div>
                </div>

NEW JQuery: 新的jQuery:

    //$('.project').mouseover(function(){
    //    $(this).prev().show()
    //})  
    //$('.project').mouseout(function(){
    //    $(this).prev().hide()
    //})  

    $('.project').click(function(){
        var id = $(this).attr("id");
        hideOthers(id);
        $(this).prev().show();
    });

    function hideOthers(id){
        $('.project').not('#' + id).prev().hide();
    }

^The problem here is that the number is no longer visible when hovering over the project. ^这里的问题是,将鼠标悬停在项目上时,该数字不再可见。 And the number does not go away if I click on the same project title again. 如果再次单击相同的项目标题,该数字也不会消失。

You don't need to use javascript to achieve this, looking at your code (on the website you referenced), once the accordion is open, an open class is set. 您不需要使用javascript来实现此目的,只需查看代码(在您引用的网站上),一旦手风琴打开,便会设置一个开放类。 So you can use css selectors to set the visibility of the numbers like this: 因此,可以使用css选择器设置数字的可见性,如下所示:

/* Show on hover */
[data-accordion]:hover > .number {display:block}

/* Show when clicked/open */
[data-accordion].open >  .number {display:block}

Try this 尝试这个

var flag = false;

$('.project').mouseover(function(){
    if(flag == false)
       $(this).prev().show()
})  
$('.project').mouseout(function(){
    if(flag == false)
       $(this).prev().hide()
})  
$('.project').click(function(){
    if(flag == false){
        $(this).prev().show()
    }else{
        $(this).prev().hide()
    }
    flag = !flag;
})

This gets the id of the clicked element and shows the previous element, while hiding the others. 这将获得clicked元素的ID ,并显示前一个元素,同时隐藏其他元素。

Hope this helps! 希望这可以帮助!



    $('.project').click(function(){
      var id = $(this).attr("id");
      hideOthers(id);
      $(this).prev().toggle();
    });

    function hideOthers(id){
      $('.project').not('#' + id).prev().hide();
    }

Here is a fiddle 这是一个小提琴

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

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