Jquery 在文档准备就绪时使用数据属性值添加类
大家好,我正在尝试使用我的html的数据属性,我会在加载页面时添加类 这是我的JqueryJquery 在文档准备就绪时使用数据属性值添加类,jquery,html5-data,Jquery,Html5 Data,大家好,我正在尝试使用我的html的数据属性,我会在加载页面时添加类 这是我的Jquery var $this = $(this); $(document).ready(function(){ $("[data-load-animation]").each(function() { $this.hide(); var cls = $this.attr("data-load-animation"); console.log("console:
var $this = $(this);
$(document).ready(function(){
$("[data-load-animation]").each(function() {
$this.hide();
var cls = $this.attr("data-load-animation");
console.log("console: "+cls);
$this.addClass(cls);
})
})
这是我的
我需要为每个具有此数据属性的元素添加类反弹,我认为这是正确的,但它不起作用,请帮助我解决问题。问题似乎是
$this
引用,在您的情况下,它引用的是窗口
对象
相反,您需要引用当前的[数据加载动画]
元素,因此在each()
循环中定义它
$(document).ready(function () {
$("[data-load-animation]").each(function () {
var $this = $(this);
$this.hide();
var cls = $this.data("loadAnimation");
console.log("console: " + cls);
$this.addClass(cls);
})
})
您正在错误放置
$this
请尝试以下操作,其中开头的$this
指的是当前窗口
$("[data-load-animation]").each(function () {
$this.hide();
var cls = $(this).data("load-animation"); // Use data instead attr
console.log("console: " + cls);
$(this).addClass(cls); // Change to $(this) instead $this
})
喜欢吗?看看
$这个
和$(这个)
谢谢你帮了大忙。-)