开发者

jQuery find this in dom

开发者 https://www.devze.com 2023-02-16 12:56 出处:网络
lets say you set var $this = jQuery(this); how can you search the dom for that element.开发者_如何转开发 $this.size()Does not work because it\'s not searching the dom for that element.I need to basica

lets say you set var $this = jQuery(this); how can you search the dom for that element.开发者_如何转开发 $this.size() Does not work because it's not searching the dom for that element. I need to basically detect if $this has been removed from the dom and stop an interval.

Update code requested:


    jQuery(document).find('div.ctalk').each(function(){
        var delay;
        var $this = this;

        activateLive = function($this) {


            clearInterval(delay);
            delay = setInterval(function(){

                if(jQuery($this).size() != '0') {

                    fn.actJSON('0', $this, 'update=1&');

                }else {
                    alert('kill it');
                    clearInterval(delay);
                }

            }, 10000 ); 

        }


        if(!jQuery(this).data('init')) {

            jQuery(this).data('init', '1'); 
            fn.activateBinds($this);
            activateLive($this);
        }

    });


This would probably be the first time I've advocated the use of .is() for anything, but it does make this quite a simple task:

if (!$(this).parents().is("body")) {
    // Node has been disconnected
}

You can swap "body" for "html" if you want to check nodes anywhere in the document.

IE also has a proprietary sourceIndex property that would do the job with slightly better performance:

if (this.sourceIndex == -1 || !$(this).parents().is("body")) {
    // Node has been disconnected
}

Extra Update: I knew there was a couple of other methods for this, they were scratching at the back of my mind but I couldn't quite remember. Modern browsers have the DOM Level 3 method .compareDocumentPosition(), so you can do the whole thing without jQuery if anyone ever needs to.

if (this.sourceIndex == -1 || document.compareDocumentPosition(this) & 0x01) {
    // Node has been disconnected
}

The other method I was thinking of is IE's .contains() method, which also works. However, using that would require a check that the function is defined first so it's easier to just stick with sourceIndex.


I need to basically detect if $this has been removed from the dom and stop an interval.

See update below, the code you posted later changes things slightly

Okay, so you have a reference to the element in this, and you want to know if it's been removed from the DOM (optionally using jQuery). This should do it:

Without jQuery:

if (!this.id) {
    this.id = "__fluglehorn__" + new Date().getTime(); // See note below
}
if (!document.getElementById(this.id)) {
    // It's not in the DOM tree
}

With jQuery:

if (!this.id) {
    this.id = "__fluglehorn__" + new Date().getTime(); // See note below
}
if ($("#" + this.id).length == 0) {
    // It's not in the DOM tree
}

Technically I may not be creating unique IDs there, and you'll want to adjust the flagged lines (if necessary) to ensure that nothing else in your page is using that prefix. But you get the idea: If the element already has an ID, use it; otherwise assign one that isn't already in use. Then look it up.


Update: Your posted code changes things very slightly (you're starting with a jQuery object in $this, not a raw element in this):

Without jQuery (for the check):

var elm = $this[0];
if (!elm.id) {
    elm.id = "__fluglehorn__" + new Date().getTime(); // See note above
}
if (!document.getElementById(elm.id)) {
    // It's not in the DOM tree
}

With jQuery:

var elm = $this[0];
if (!elm.id) {
    elm.id = "__fluglehorn__" + new Date().getTime(); // See note above
}
if ($("#" + elm.id).length == 0) {
    // It's not in the DOM tree
}

Example function for checking:

// Checks the given element (or the first element in the given jQuery object)
// to see if it's in the DOM tree
function inDomTree(elm) {
    var rv, id, counter;

    rv = false;
    if (elm && elm.jquery) {
        elm = elm[0];
    }
    if (elm) {
        if (!elm.id) {
            counter = 0;
            do {
                id = "__fakeid__" + new Date().getTime() + "_" + ++counter;
            } while (document.getElementById(id));
            elm.id = id;
        }
        rv = !!document.getElementById(elm.id);
        if (id) {
            this.id = "";
        }
    }
    return rv;
}


$this, is an object, so it will also have the attributes of the element you want to search.. you can try something like this: $("#"+$this.attr('id')) to get the element back, else you can also use a class the element has..!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号