首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jQuery使用$(this)作为setInterval/setTimeOut函数中的起点遍历DOM?

jQuery使用$(this)作为setInterval/setTimeOut函数中的起点遍历DOM?
EN

Stack Overflow用户
提问于 2021-05-08 05:25:53
回答 1查看 63关注 0票数 0

我一直试图理解如何(在jQuery中)使用$(this)作为setInterval/setTimeOut函数中的起点遍历DOM,并遇到了一些令人困惑的行为。

使用$(this)作为setInterval/setTimeOut函数中的起点遍历DOM似乎是不可能的

为什么会这样呢?

jQuery/javaScript人员--了解这一点。

说明行为的示例代码:

jQuery:

代码语言:javascript
运行
复制
// set this code anywhere (inside/outside - document.ready/named-function/...) and run it
var autoTimer = setInterval(function(){
    // INSERT EXAMPLE TEST-CODE FROM BELOW HERE
}, 10000)

测试代码尝试:

代码语言:javascript
运行
复制
// from inside the setInterval/setTimeOut function above
// if $(this)
if($(this).length !== 0) {
    alert('hello');
} // alerts hello

// from inside the setInterval/setTimeOut function above
// if html-tag (body/head/html)
if($('body').length !== 0) {
    alert('hello');
} // alerts hello

// from inside the setInterval/setTimeOut function above
// if className
if($('.className').length !== 0) {
    alert('hello');
} // alerts hello

// from inside the setInterval/setTimeOut function above
// if id
if($('#id').length !== 0) {
    alert('hello');
} // alerts hello

因此,它发现文档中的特定元素很好,除了$(这个)之外,您还可以从那里遍历DOM。

即。

代码语言:javascript
运行
复制
// from inside the setInterval/setTimeOut function above
if ($('.className').find(something).length !== 0)... // works just fine

当您希望使用$(this)作为遍历的起点时,问题就出现了。

遍历测试代码时尝试使用$(此)作为起点:

代码语言:javascript
运行
复制
// from inside the setInterval/setTimeOut function above
// traversing up the DOM
if($(this).parents('body').length !== 0) {
    alert('hello');
} // no alert (same with closest/parentsUntil/etc up the DOM)

// from inside the setInterval/setTimeOut function above
// traversing sideways in the DOM
if($(this).siblings('body').length !== 0) {
    alert('hello');
} // no alert (same with next/prev/etc sideways in the DOM)

// from inside the setInterval/setTimeOut function above
// traversing down the DOM
if($(this).find('body').length !== 0) {
    alert('hello');
} // no alert (same with children down the DOM)

因此,从$(this) 'body‘(或其他任何事情)开始,似乎不存在任何地方(向上/向下/横向),从上面的任何其他起点来看,这都不是真的。

我也尝试使用一个命名的全局函数,如:

代码语言:javascript
运行
复制
// set outside/inside document.ready
function testCode() {
    // INSERT EXAMPLE TRAVERSING TEST-CODE FROM ABOVE HERE
}

// set this code anywhere (inside/outside - document.ready/named-function/...) and run it
var autoTimer = setInterval(testCode, 10000) // no alert

// set this code anywhere (inside/outside - document.ready/named-function/...) and run it
var autoTimer = setInterval(function(){
    testCode();
}, 10000) // no alert

所以再重复我的问题:

为什么在jQuery中不可能使用$(this)作为起点来遍历setInterval/setTimeOut内的DOM (上/下/侧)?

对于这种行为,有什么可能的黑客/工作场所/等等?

可能的无-解决方案的黑客/工作-四周围/等.

使用$.proxy的这个答案只在使用较早版本的jQuery时才能工作--但由于正如这份文件所证明的‘在jQuery 3.3中已不再推荐使用该API’.目前,这不是一个解决办法。

绑定方法也是如此,因为文档证明“截至jQuery 3.0,.bind()已被废弃”,并被在……上面方法所取代(但我无法看出在这里如何使用.on() )--但这可能只是因为我缺乏想象力)。

EN

Stack Overflow用户

回答已采纳

发布于 2021-05-08 07:25:00

这是一个基本的this范围问题,与jQuery没有什么关系。

最简单的方法是将this分配给回调之外的另一个变量:

代码语言:javascript
运行
复制
var that = this;

setInterval(function () {
    $(that).parents();
}, 1000);

如果您不关心IE,或者您有一个构建过程,您可以使用箭头函数,它从外部的作用域使用this

代码语言:javascript
运行
复制
setInterval(() => {
    $(this).parents();
}, 1000);

我个人的偏好是完全避免使用this,因为它的所有令人垂头丧气的行为让你非常悲伤。从您的示例中还不清楚this值来自何处,但以下是一些可以避免的方法:

代码语言:javascript
运行
复制
// inside .each()
$('a').each(function (_, el) {
    $(el).parents();
});

// inside event handler
$('a').on('click', function (event) {
    $(event.target).parents();
});
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67444355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档