복붙노트

[JQUERY] jQuery를에 인덱스 요소를 가져옵니다

JQUERY

jQuery를에 인덱스 요소를 가져옵니다

해결법


  1. 1.

    $(...)[index]      // gives you the DOM element at index
    $(...).get(index)  // gives you the DOM element at index
    $(...).eq(index)   // gives you the jQuery object of element at index
    

    DOM 객체는, CSS 기능이 마지막을 사용하지 않는 ...

    $('ul li').eq(index).css({'background-color':'#343434'});
    

    문서 :

    (색인) 반환 갔지 : 요소

    .EQ (색인) 반환 값 : jQuery를


  2. 2.당신은 특정 인덱스에있는 요소를 얻기 위해 jQuery의 .EQ () 메서드를 사용할 수 있습니다.

    당신은 특정 인덱스에있는 요소를 얻기 위해 jQuery의 .EQ () 메서드를 사용할 수 있습니다.

    $('ul li').eq(index).css({'background-color':'#343434'});
    

  3. 3.당신은 EQ 방법 또는 선택기를 사용할 수 있습니다 :

    당신은 EQ 방법 또는 선택기를 사용할 수 있습니다 :

    $('ul').find('li').eq(index).css({'background-color':'#343434'});
    

  4. 4.CSS를 사용하여 jQuery를에 인덱스로 요소를 얻는 또 다른 방법이있다 : n 번째-of-type 가상 클래스는 :

    CSS를 사용하여 jQuery를에 인덱스로 요소를 얻는 또 다른 방법이있다 : n 번째-of-type 가상 클래스는 :

    <script>
        // css selector that describes what you need:
        // ul li:nth-of-type(3)
        var selector = 'ul li:nth-of-type(' + index + ')';
        $(selector).css({'background-color':'#343434'});
    </script>
    

    당신은 당신이 필요로 모든 요소와 일치하도록 jQuery를 함께 사용할 수있는 다른 선택기가 있습니다.


  5. 5.당신은 JQuery와 그냥 사용하는 CSS 스타일 태그를 건너 뛸 수 :

    당신은 JQuery와 그냥 사용하는 CSS 스타일 태그를 건너 뛸 수 :

     <ul>
     <li>India</li>
     <li>Indonesia</li>
     <li style="background-color:#343434;">China</li>
     <li>United States</li>
     <li>United Kingdom</li>
     </ul>
    
  6. from https://stackoverflow.com/questions/9887534/get-an-element-by-index-in-jquery by cc-by-sa and MIT license