如何从jquery中内的指定获取最近的左或右 id

时间:2022-11-27 21:34:21

Here is my html structure

这是我的html结构

<ul id="slider_donor" class="clearfix">
  <li id="slider_3">
  <li id="slider_4">
  <li id="slider_5">
  <li id="active-slide" class="active-slide">
  <li id="slider_6">
  <li id="slider_1">
  <li id="slider_2">
</ul>

Now I want to get the id closest to <li id="active-slide" class="active-slide"> form left or from right .

现在我希望从左侧或右侧获得最接近

  • 表单的ID。

  • That means from left id should be slider_5 and from right it should be slider_6

    这意味着从左边的id应该是slider_5,从右边它应该是slider_6

    5 个解决方案

    #1


    4  

    Use .next() or .prev():

    使用.next()或.prev():

     $('#active-slide').prev().prop('id'); // slider_5
     $('#active-slide').next().prop('id'); // slider_6
    

    #2


    2  

    $('.active-slide').next().attr('id');
    $('.active-slide').prev().attr('id');
    

    #3


    2  

    jsfiddle here

    use .next() and .prev()

    使用.next()和.prev()

    $('.active-slide').next().css('background-color','red');
    $('.active-slide').prev().css('background-color','green');
    
    <ul id="slider_donor" class="clearfix">
        <li id="slider_3">xx</li>
      <li id="slider_4">xx</li>
      <li id="slider_5">xx</li>
      <li id="active-slide" class="active-slide">xx</li>
      <li id="slider_6">xx</li>
      <li id="slider_1">xx</li>
      <li id="slider_2">xx</li>
    </ul>
    

    #4


    1  

    You can use .prev('li') and .next('li') selector for that
    

    https://api.jquery.com/prev/

    http://api.jquery.com/next/

    $('active-slide').prev('li').prop('id'); // slider_5
     $('#active-slide').next('li').prop('id'); // slider_6
    

    #5


    0  

    .prev and .next are your friend.

    .prev和.next是你的朋友。

    var leftId, rightId, left, right;
    var left = $(".active-slide").prev("li");
    var right = $(".active-slide").next("li");
    if (left.length > 0) {
      leftId = left.attr("id");
    }
    if (right.length > 0) {
      rightId = right.attr("id");
    }
    

    #1


    4  

    Use .next() or .prev():

    使用.next()或.prev():

     $('#active-slide').prev().prop('id'); // slider_5
     $('#active-slide').next().prop('id'); // slider_6
    

    #2


    2  

    $('.active-slide').next().attr('id');
    $('.active-slide').prev().attr('id');
    

    #3


    2  

    jsfiddle here

    use .next() and .prev()

    使用.next()和.prev()

    $('.active-slide').next().css('background-color','red');
    $('.active-slide').prev().css('background-color','green');
    
    <ul id="slider_donor" class="clearfix">
        <li id="slider_3">xx</li>
      <li id="slider_4">xx</li>
      <li id="slider_5">xx</li>
      <li id="active-slide" class="active-slide">xx</li>
      <li id="slider_6">xx</li>
      <li id="slider_1">xx</li>
      <li id="slider_2">xx</li>
    </ul>
    

    #4


    1  

    You can use .prev('li') and .next('li') selector for that
    

    https://api.jquery.com/prev/

    http://api.jquery.com/next/

    $('active-slide').prev('li').prop('id'); // slider_5
     $('#active-slide').next('li').prop('id'); // slider_6
    

    #5


    0  

    .prev and .next are your friend.

    .prev和.next是你的朋友。

    var leftId, rightId, left, right;
    var left = $(".active-slide").prev("li");
    var right = $(".active-slide").next("li");
    if (left.length > 0) {
      leftId = left.attr("id");
    }
    if (right.length > 0) {
      rightId = right.attr("id");
    }