jQuery 在 dom 中找到这个
假设您设置了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能是我第一次提倡使用
.is()
< /a> 任何东西,但这确实使这成为一项非常简单的任务:如果您想在任何地方检查节点,您可以将
"body"
替换为"html"
在文档中。IE 还有一个专有的
sourceIndex
属性可以以稍微更好的性能完成这项工作:
额外更新:我知道还有一些其他方法可以实现这一点,它们在我脑海中浮现,但我不太记得了。现代浏览器具有 DOM Level 3 方法
.compareDocumentPosition()
,所以如果有人需要的话,你可以在没有 jQuery 的情况下完成整个事情。我想到的另一种方法是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: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: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.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 withsourceIndex
.请参阅下面的更新,您稍后发布的代码略有更改
好的,您在
this
中引用了该元素,并且您想知道它是否已从DOM(可以选择使用 jQuery)。这应该可以做到:没有 jQuery:
使用 jQuery:
技术上 我可能不会在那里创建唯一的 ID,并且您需要调整标记行(如果需要)以确保页面中没有其他内容使用该前缀。但你明白了:如果元素已经有 ID,就使用它;如果该元素已经有 ID,就使用它;否则分配一个尚未使用的值。然后查一下。
更新:您发布的代码稍有改变(您从
$this
中的 jQuery 对象开始,而不是this
中的原始元素) :不使用 jQuery(用于检查):
使用 jQuery:
用于检查的示例函数:
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:
With jQuery:
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 inthis
):Without jQuery (for the check):
With jQuery:
Example function for checking:
$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..!