jQuery object equality

前端 未结 7 1751
醉梦人生
醉梦人生 2020-11-22 14:37

How do I determine if two jQuery objects are equal? I would like to be able to search an array for a particular jQuery object.

$.inArray(jqobj, my_array);//-         


        
7条回答
  •  悲哀的现实
    2020-11-22 14:54

    It is, generally speaking, a bad idea to compare $(foo) with $(foo) as that is functionally equivalent to the following comparison:

    
    
    
    
    
    
    

    Of course you would never expect "JS engine screw-up". I use "$" just to make it clear what jQuery is doing.

    Whenever you call $("#foo") you are actually doing a jQuery("#foo") which returns a new object. So comparing them and expecting same object is not correct.

    However what you CAN do may be is something like:

    
    
    
    
    
    
    

    So really you should perhaps compare the ID elements of the jQuery objects in your real program so something like

    ... 
    $(someIdSelector).attr("id") == $(someOtherIdSelector).attr("id")
    

    is more appropriate.

提交回复
热议问题