1

所以我试图让 $(this) 工作。

这不起作用:

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

但这确实:

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

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

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

我是否缺少 $(this) 和 .data 的一些重要部分?

4

1 回答 1

3

在参数语句中使用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
});
于 2014-03-01T01:02:33.967 回答