日期:2014-05-16  浏览次数:20343 次

jquery选择器是同一个对象为什么不相等
[code=html
<select>
<option>列表1</option>
<option>列表2</option>
<option>列表3</option>
<option>列表4</option>
<option>列表5</option>
</select>
[/code]
采用jquery

var $select1=$("select");
var $select2=$("select");
alert($select1==$select2);//false
alert($select1[0]==$select2[0]);//true


为什么jquery对象不相等,dom对象相等?求大神帮忙
jquery?dom?对象?相等 jquery dom 对象 相等,

------解决方案--------------------
说了也不懂,没事找jQuery出来干嘛?
function EqOrN(){

}
var $a = new EqOrN();
var $b = new EqOrN();
var $c = ($a==$b);


a = new EqOrN();
function gE(){
return a;
}

var $d = gE();
var $e = gE();
var $f = ($d == $e);

var $g = new gE();
var $h = new gE();
var $i = ($g==$h);

------解决方案--------------------
引用:
Quote: 引用:




var $select1=$("select");//返回的是数组[]
var $select2=$("select");//返回的是数组[]
alert($select1==$select2);//[]==[]数组返回false
alert($select1[0]==$select2[0]);//数组的第一个对象都是指向同一个引用对象

javascript为什么相等的呢

var p1=document.getElementsByTagName("select");
var p2=document.getElementsByTagName("select");
alert(p1==p2);//true p1、p2是数组

你这里取到的p1 p2虽然有length的属性,但并非数组,而是一个类数组的东西,dom中称作NodeList。你可以用数组的push,pop等方法检测一下。

至于用jquery取,取出来的是两个对象,只不过是有一样的属性和方法而已。
比如说一对一模一样的双胞胎(A和B),你能说A就是B,B就是A吗~~