English 中文(简体)
页: 1 展示/展示纤维元素的单子 lo
原标题:jQuery one button loop to show/hide sibling elements
  • 时间:2012-04-17 21:44:18
  •  标签:
  • jquery
  • loops

我试图利用一个纽子,通过一个无顺序的分子清单,以假装的方式展示/杀害。 显示第1个元件,点击纽顿,显示的元件是隐蔽的,下一个元件显示......在滑坡how下点击。 问题在于,如果发言,我不知道如何在第一次发言之后停止或重新启动点击功能。 现在,错开这一职能就贯穿了它们。 谁能看我的法典,给我一手?

var daily =  .daily_goal_activity li ;   

$(daily).addClass(function (index) {
        return "item-" + index;
    });
    $( .item-0 ).show();
});

$( #activity_toggle ).click(function () {
    $(daily).hide();

    if ($(daily).hasClass( item-0 )) {
        $( .item-1 ).show();
    }
    if ($(daily).hasClass( item-1 )) {
        $( .item-2 ).show();
    }
});
最佳回答

如果它们是清单内容,那么它们是兄弟姐妹,是正确的?

你只能提到目前显而易见的内容,隐藏着这个内容,并展示下一个兄弟情谊。

假设所有清单内容最初都是隐蔽的,只有“<现值<<>>>>/代码”的元件即可识别,因此,情况如下:

var $daily = $( .daily_goal_activity li );
$daily.first().addClass( current );

$( #activity_toggle ).click(function () {
    $daily.filter( .current )
      .removeClass( current )
      .next( li ).addClass( current );
});

If you want to cycle through the elements, you can check whether there actually is a next sibling and if not, go back to the first one:

var $next = $daily.filter( .current )
    .removeClass( current )
    .next( li );

if($next.length === 0) {
    $next = $daily.first();
}

$next.addClass( current );

rel=“nofollow”>DEMO

问题回答

几个要点。

  1. Storing .daily_goal_activity li and reusing it doesn t really save you anything. You really want to store the selector and reuse that: var daily = $( .daily_goal_activity li );
  2. .hasClass( item-0 ) will always be true, because one of the lis will have the class item-0, as you applied that class in .addClass().
  3. To make your code work in its current form, you want to hide x and show x+1. You could use a global var to track x.
  4. You are better off using the structure of the html to track next/previous for you:

var daily = $( .daily_goal_activity li );

daily.hide().first().show();

$( #activity_toggle ).click(function () {
    var nextdaily = daily.filter( :visible ).hide().next();
    if(nextdaily.length > 0) {
        nextdaily.show();
    } else {
        daily.first().show();
    }
});

Demo: http://jsfiddle.net/jtbowden/yRFba/

单纽芬兰

  <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $("button").click(function(){
        var id=$("button").attr("id");
        if(id=="hide")
        {
                $("#example").hide();
                $(this).html("Show");
                $(this).attr({"id":"show"});

        }
        else
        {
             $("#example").show();
             $(this).html("Hide");
             $(this).attr({"id":"hide"});
        }



      });
    });
    </script>
    </head>

    <body>
    <button id="hide">Hide</button>
    <p id="example">Hide me out</p>
    </body>
    </html>




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签