开发者

How to get the ID of an LI with the ID of the previous LI with jquery

I was wondering if anyone knows if its possible to get what the ID of the next LI is with only using the ID of the previous LI.

For exam开发者_Python百科ple

So i would want to be able to go function getNextLI(currentliID){ $(li#currentliID:next-child).animate({ opacity: 1 }); }

All the LI id's won't be in order as they are the item ids coming from the mysql database.


you can use the next() method of jQuery. For example:

<ul>
    <li>...</li>
    <li id-"test">...</li>
    <li>NEXT</li>
    <li>...</li>
</ul>

The following will get you the li containing the word "NEXT":

var nextSibling = $("li#test").next();


This will fix your problem:

var getNextLI = function(id) {
  return $('li#' + id).next('li');
};

var anaimateNext = function(id) {
  getNextLI(id).animate({ opacity: 1});
}


You can use jQuery(this).next()

<ol>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
</ol>


jQuery(function(){

    jQuery('ol li').bind('click',function(){
        jQuery('ol li').css("background-color","#ffffff");
        jQuery(this).next().css("background-color","red");
    });

});

http://jsfiddle.net/BzFfM/2/


<script>

jQuery(function(){

jQuery('ol li').bind('click',function(){

var s = jQuery(this).next().attr("id");

});

});

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜