How do I compare two jQuery objects for identity?

后端 未结 3 1566
谎友^
谎友^ 2020-12-01 13:36

I\'m trying to use jQuery to open / close control \'boxes\' on a webpage. Unfortunately, it doesn\'t look very good to close a box just to re-open it if the user happens to

相关标签:
3条回答
  • 2020-12-01 14:11

    You can also do:

     if(val.is(this))
    
    0 讨论(0)
  • 2020-12-01 14:12

    Try this:

    function openBox(index)
    {
    val=$('#box'+index);
    $('.profilePageContentBox').each(function(){
        if($(this).is(":visible"))
        {
            if(!$(this).is("#box"+index))
                $(this).slideToggle(200);
        }
    });
    val.slideToggle(200);
    }
    
    0 讨论(0)
  • 2020-12-01 14:20

    Using the $() function will always create a new object, so no matter what, your equality check there will always fail.

    For example:

    var div = document.getElementById('myDiv');
    
    $(div) === $(div);   // false!
    

    Instead, you could try just storing the actual DOM elements, since those are just referred to inside jQuery objects.

    val = $('#box'+index).get(0);
    ...
    if (this !== val) { }
    
    0 讨论(0)
提交回复
热议问题