所以我正在尝试让 $(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/