jQuery 在 dom 中找到这个

发布于 2024-10-21 05:51:44 字数 871 浏览 7 评论 0原文

假设您设置了 var $this = jQuery(this); 如何在 dom 中搜索该元素。 $this.size() 不起作用,因为它没有在 dom 中搜索该元素。我基本上需要检测 $this 是否已从 dom 中删除并停止一个间隔。

请求更新代码:


    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);
        }

    });

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);
        }

    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

客…行舟 2024-10-28 05:51:44

这可能是我第一次提倡使用 .is()< /a> 任何东西,但这确实使这成为一项非常简单的任务:

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

如果您想在任何地方检查节点,您可以将 "body" 替换为 "html" 在文档中。

IE 还有一个专有的 sourceIndex属性可以以稍微更好的性能完成这项工作:

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

额外更新:我知道还有一些其他方法可以实现这一点,它们在我脑海中浮现,但我不太记得了。现代浏览器具有 DOM Level 3 方法 .compareDocumentPosition(),所以如果有人需要的话,你可以在没有 jQuery 的情况下完成整个事情。

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

我想到的另一种方法是IE的 .contains() 方法,也有效。但是,使用它需要首先检查该函数是否已定义,因此更容易坚持使用 sourceIndex

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.

独自唱情﹋歌 2024-10-28 05:51:44

我基本上需要检测 $this 是否已从 dom 中删除并停止一个间隔。

请参阅下面的更新,您稍后发布的代码略有更改

好的,您在 this 中引用了该元素,并且您想知道它是否已从DOM(可以选择使用 jQuery)。这应该可以做到:

没有 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
}

使用 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
}

技术上 我可能不会在那里创建唯一的 ID,并且您需要调整标记行(如果需要)以确保页面中没有其他内容使用该前缀。但你明白了:如果元素已经有 ID,就使用它;如果该元素已经有 ID,就使用它;否则分配一个尚未使用的值。然后查一下。


更新:您发布的代码稍有改变(您从 $this 中的 jQuery 对象开始,而不是 this 中的原始元素) :

不使用 jQuery(用于检查):

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
}

使用 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
}

用于检查的示例函数:

// 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;
}

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;
}
独自唱情﹋歌 2024-10-28 05:51:44

$this, 是一个对象,因此它也会具有您要搜索的元素的属性。您可以尝试这样的操作: $("#"+$this.attr('id')) 来获取元素回来,否则你也可以使用元素具有的类..!

$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..!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文