.get (index)

· 8년 전 · 1978

.get (index)


설명 : jQuery 객체와 일치하는 요소 중 하나를 검색하십시오.


이 .get()메소드는 각 jQuery 객체의 기본 DOM 노드에 대한 액세스를 허용합니다. 의 값이 index범위를 벗어나는 경우 - 음의 요소 수보다 작거나 요소 수보다 크거나 같으면 반환합니다 undefined. 순서가없는 간단한 목록을 생각해보십시오.


<ul>

  <li id="foo">foo</li>

  <li id="bar">bar</li>

</ul>

인덱스를 지정 .get( index )하면 단일 요소를 검색합니다.


console.log( $( "li" ).get( 0 ) );

인덱스는 0부터 시작하므로 첫 번째 목록 항목이 반환됩니다.


<li id ​​= "foo">


또한 각 jQuery 객체는 배열로 위장하므로 배열 역 참조 연산자를 사용하여 목록 항목을 대신 얻을 수 있습니다.

console.log( $( "li" )[ 0 ] );

그러나이 구문에는 음수 인덱스 지정과 같은 .get ()의 추가 기능이 없습니다.


console.log( $( "li" ).get( -1 ) );

음수 인덱스는 일치하는 집합의 끝에서부터 계산되므로이 예제는 목록의 마지막 항목을 반환합니다.


<li id ​​= "bar">


예:

클릭 요소의 태그 이름을 표시합니다.


<!doctype html>

<html lang="en">

<head>

  <meta charset="utf-8">

  <title>get demo</title>

  <style>

  span {

    color: red;

  }

  div {

    background: yellow;

  }

  </style>

  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

</head>

<body>

 

<span>&nbsp;</span>

<p>In this paragraph is an <span>important</span> section</p>

<div><input type="text"></div>

 

<script>

$( "*", document.body ).click(function( event ) {

  event.stopPropagation();

  var domElement = $( this ).get( 0 );

  $( "span:first" ).text( "Clicked on - " + domElement.nodeName );

});

</script>

 

</body>

</html>

|
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
jQuery 8년 전 조회 2,473
jQuery
[jQuery]
8년 전 조회 1,748
jQuery 8년 전 조회 1,454
jQuery
[jQuery]
8년 전 조회 1,893
jQuery 8년 전 조회 5,469
기타 8년 전 조회 3,220
jQuery
[jQuery]
8년 전 조회 1,596
jQuery 8년 전 조회 1,765
jQuery 8년 전 조회 1,869
PHP 8년 전 조회 5,196
node.js 8년 전 조회 4,195
jQuery 8년 전 조회 2,256
jQuery 8년 전 조회 1,703
jQuery 8년 전 조회 1,260
jQuery 8년 전 조회 1,979
jQuery 8년 전 조회 1,472
jQuery 8년 전 조회 1,589
jQuery 8년 전 조회 1,803
jQuery 8년 전 조회 2,050
jQuery 8년 전 조회 1,592
jQuery 8년 전 조회 1,521
jQuery 8년 전 조회 1,605
jQuery
[jQuery]
8년 전 조회 1,475
jQuery 8년 전 조회 1,622
jQuery 8년 전 조회 1,678
jQuery 8년 전 조회 1,683
jQuery 8년 전 조회 1,865
jQuery 8년 전 조회 1,805
jQuery 8년 전 조회 1,665
jQuery 8년 전 조회 1,734
🐛 버그신고