您的位置:首页 > Web前端 > JQuery

jQuery : eq() vs get()

2015-12-24 18:22 726 查看

.get(index)
and
.eq(index)

both return a single "element" from a jQuery object array, but they return the single element in different forms.

.eq(index)


returns it as a jQuery object, meaning the DOM element is wrapped in the jQuery wrapper, which means that it accepts jQuery functions.

.get(index)


return a raw DOM element. You may manipulate it by accessing its attributes and invoking its functions as you would on a raw DOM element. But it loses its identity as a jQuery-wrapped object, so a jQuery function
 won't work.

简言之:eq()获取的是jquery对象,能够使用jquery方法,get()获取的是原生dom元素,不能使用jquery方法.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>eq() And get()</title>
</head>
<body style="height:2000px;">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<div id="outer_div">
<div>
<input text value="AAA"><br>
<input text value="BBB"><br>
<input text value="CCC"><br>
<input text value="DDD"><br>
</div>
</div>
<script>
$(document).ready(function(){
var $inputEq = $('#outer_div').find("input").eq(2);
var $inputGet = $('#outer_div').find("input").get(2);
console.log("inputEq :"+$inputEq);
console.log("inputGet:"+$inputGet);
console.log("inputEqValue :"+$inputEq.val());
console.log("inputGetValue :"+$inputGet.value);
});
// Results:
/*
inputEq :[object Object]
inputGet:[object HTMLInputElement]
inputEqValue :CCC
inputGetValue :CCC
*/
</script>
</body>
</html>


相关:

.eq()

.eq( index )

.eq( indexFromEnd )

<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
$( "li" ).eq( 2 ).css( "background-color", "red" );
$( "li" ).eq( -2 ).css( "background-color", "blue" );


:eq() Selector

jQuery( ":eq(index)" )

jQuery( ":eq(-index)" )

.get()

.get( index )

<ul>
<li id="foo">foo</li>
<li id="bar">bar</li>
</ul>
console.log( $( "li" ).get( 0 ) );


.get()

Retrieve the elements matched by the jQuery object.

All of the matched DOM nodes are returned by this call, contained in a standard array.

<ul>
<li id="foo">foo</li>
<li id="bar">bar</li>
</ul>
// javascript
console.log( $( "li" ).get() );
// result
[<li id="foo">, <li id="bar">]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: