check if cached jquery object is still in DOM

前端 未结 4 965
情书的邮戳
情书的邮戳 2021-01-04 02:52

Does anyone know how to tell if a cached jQuery object has gone stale, e.g. is no longer in the DOM? For example:

var $cached_elem = $(\'.the_button\');

//         


        
4条回答
  •  离开以前
    2021-01-04 03:09

    The native document.contains() method should be faster than jQuery to determine if a cached jQuery element exists in the DOM.

    if (document.contains($cached_elem[0])) {
        // Element is still in the DOM
    }
    

提交回复
热议问题