【问题标题】:jQuery find this in domjQuery 在 dom 中找到这个
【发布时间】:2011-03-10 12:50:22
【问题描述】:

假设您设置了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); } });

【问题讨论】:

  • 请提供一些代码。 jsfidddle.net
  • if(jQuery($this).size() != '0') {} 不起作用,因为它没有在 dom 中搜索元素。
  • 仅供参考,没有充分的理由使用jQuery 代替$。如果您出于某种原因使用$.noConflict,您仍然可以将代码包装到使$ 可用的函数中:(function($){ /* your code using $ */ })(jQuery);
  • @ThiefMaster:也许他只是更喜欢清晰。或者他在相同的代码(不只是同一个页面)中同时使用了jQuery和Prototype/MooTools/X。
  • 在后一种情况下,他可以使用例如$j 用于 jQuery ;)

标签: jquery find this


【解决方案1】:

这可能是我第一次提倡使用.is() 做任何事情,但这确实让这件事变得非常简单:

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

【讨论】:

  • 我非常爱你,你真棒我的朋友上帝保佑你。罗夫
【解决方案2】:

我基本上需要检测 $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,就使用它;否则分配一个尚未使用的。然后查一下。


更新:您发布的代码略有改变(您从 $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;
}

【讨论】:

  • 这很好用,但是如果同时删除两个相同的对象,那么它们最终可能具有相同的 id 对吗?将 dom 树中的 $this 的数量添加到 Id 会不会很痛苦,我将如何做到这一点。我需要获取该类出现的数量,所以如果一个页面上有三个带有 ctalk 类的 div,那么每个应该包含 1、2 或 3。我希望我有道理,我太累了。
  • @kr1zmo:我可能会将检查封装在一个函数中,并让函数循环只添加一个循环计数器。我将添加一个示例。
  • @T.J. Crowder,你的和@Andy E 的例子都完美无缺。天才
  • @T.J. Crowder,还有一件事,是否有必要将该函数放在每个函数中的 activateLive 函数中,还是将它放在外面有相同的效果?
【解决方案3】:

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

【讨论】:

  • $("#"+$this.attr('id')) 天哪,但这还有很长的路要走...... ;-)
猜你喜欢
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 2019-10-27
相关资源
最近更新 更多