English 中文(简体)
jquery - 找到最后要素,而不是改变其等级
原标题:jquery - finding the last element and not changing it s class
  • 时间:2011-03-24 08:47:50
  •  标签:
  • jquery

JavaScript and jQuery newbie here, and I wanted to ask a question; Basically, I have a list and only 1 element of it is displayed at any given time, but the problem is - when I get to the end of the list, and I want to display the next element and hide the current element, than everything becomes hidden. here s the code:

说到底,我可以使用最后(最后)确定最后一个要素,给它一个身份证,如果该身份证是最后要素,则不执行指挥,但我确实不知道 Java的辛子。

传真:

<style>
  #
  #container li .active
  {
    display:inline-block;
  }
  #container li .inactive
  {
    display:none;
  }
</style>
</head>
<body>
  <ul id="container">
   <il class= active >item I</il>
   <il class= inactive >item II</il>
   <il class= inactive >item III</il>
   <il class= inactive >item IIII</il>
   <il class= inactive >item IV</il>
   </ul>
     <span id= next >Next</span>
     <span id= prev >Previous</span> 

<script>
$(".inactive").hide();
$(".active").show();
$("#next").click(function()
                 {
        var nexte=$(".active");
                nexte.addClass("inactive");
        nexte.removeClass("active")
        nexte.next(".inactive").addClass("active");
        nexte.next(".inactive").removeClass("inactive");
        $(".inactive").hide( slow );
        $(".active").show( slow );
});
$("#prev").click(function()
                 {
        var preve=$(".active");
                preve.addClass("inactive");
        preve.removeClass("active")
        preve.prev(".inactive").addClass("active");
        preve.prev(".inactive").removeClass("inactive");
        $(".inactive").hide( slow );
        $(".active").show( slow );
});
</script>

事先感谢它。

最佳回答

仅检查是否有下一个要素。 每个饰物都有一个财产<代码>length,表明选择了多少元素。 如果element.next(>>> 返回一个长度为0的饰物体,你知道没有下一个要素。

例:

$("#next").click(function() {
    var current = $(".active"),
        next = current.next(".inactive");

    if(next.length > 0) { // there exists a next element
        current.add(next)
          .toggleClass("active inactive")
          .toggle( slow ):
    }
});

Edit: 消除重复<>。

<<>Reference:>http://api.jquery.com/length/"rel=“nofollow”>,,<>add(>>>>/code>,

DEMO

你们也可以朝着另一个方向努力。

问题回答

一切都变得隐蔽,因为你写的法典不会给非现存元素增加一个积极的等级。

nexte.next(".inactive").addClass("active");
nexte.next(".inactive").removeClass("inactive");

当你重新列入清单的最后一项内容时,下一个要素是不存在的,因此,要求增加地图集/删除。 班级就属于真空。 如果你再次想做某种总结(如果你在最后一点点下点击,你会看到第一个要素),那么你会这样做:

var nextElement = nexte.next( .inactive );

if(nextElement.length == 0)
   nextElement = $( .inactive ).first(); // wrap to the first object, etc

nextElement.addClass( active ).removeClass( inactive );




相关问题
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: &...

热门标签