jquery find(),eq() 返回值问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Sunny__wei/article/details/79145239

之前一直以为find()函数返回的是一个数组类型,并不是jquery对象,后来在开发中发现有的代码体现的并不是数组类型,就一直存在疑惑,现在做个总结。

直接上代码

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
  $("#but").click(function(){
var $p= $("#b").find("p");
console.log($p);  //返回 m.fn.init(3).....
console.log(jQuery.type($p)); // 返回 object
console.log($p instanceof jQuery); //返回 true
console.log($p.html()); //返回 aa
console.log($p[1]); //返回 <p>bb</p>
console.log($p[1] instanceof jQuery); //返回 false
console.log($p.eq(1)); //返回 m.fn.init
console.log($p.eq(1) instanceof jQuery); //返回 true
console.log($p.eq(1).html());//返回 bb
})
})
</script>
</head>
<body id="b">
<p>aa</p>
<p>bb</p>
<p>cc</p>
<input type="button" value="点击" id="but"/>
</body>
</html>

说明 find()函数返回的是一个jquery对象数组,如果直接调用html(),val()默认取值是数组的下标位0的值, p[1]Dom p.eq(1) 返回的是一个jquery对象

猜你喜欢

转载自blog.csdn.net/Sunny__wei/article/details/79145239