jquery - 为什么 "$(this)"不能在 jquery-backstretch 上工作宽度 .data?

标签 jquery this jquery-backstretch

所以我正在尝试让 $(this) 工作。

这不起作用:

$(".tournament-thumb").backstretch($(this).data("url"));

但这确实:

$(".tournament-thumb").backstretch($(".tournament-thumb").data("url"));

我需要从数据中获取网址。这是 HTML:

<div class="tournament-thumb" data-url="{{asset('images/tournament/' . $tournament->image)}}">

我是否遗漏了 $(this) 和 .data 的一些重要部分?

最佳答案

在参数语句中使用this,不会更改其上下文。它仍然引用与周围范围内其他任何地方相同的对象

function x(){
    //this in here and the function parameter call refers to the same object
    console.log(this)
    $(".tournament-thumb").backstretch($(this).data("url"));
}

在 jQuery 中,如果您将回调传递给函数,则在这些函数内 this 很可能引用当前正在使用的 dom 元素。

如果是这样的话

$(".tournament-thumb").backstretch(function(){
    //do something
    //here this might refer to the tournament-thumb element
});

关于jquery - 为什么 "$(this)"不能在 jquery-backstretch 上工作宽度 .data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22108667/

相关文章:

javascript - 如何在 Accordion 导航菜单中设置当前页面子链接及其父链接的样式?

javascript - jQuery 查找文本并使用每个函数写入另一个标签属性

html - 更改或添加标签变量的样式?

javascript - 将引用 $(this) 保存到数据库

jquery - 后伸和背景大小 : cover

javascript - 在 backstretch 上叠加内容

javascript - 面包屑的动态宽度

javascript - AngularJS - 在 POST 请求中使用 this 与 $scope

c++ - 如何从 std::function 获取这个指针?

javascript - 为移动设备提供一组不同的图像