jQuery 此选择器不可用于 animate() 方法属性

标签 jquery jquery-animate this

有人可以向我解释一下为什么“this”选择器不可用于 animate() 方法属性。

请参阅第一个示例:http://jsfiddle.net/qZVv4/ ,没有产生任何错误,它只是忽略它。

 $('#test1 .bar[data-percentage]').animate({
     width: $(this).attr('data-percentage'),
     easing: 'easeOutBounce'
 }, 1000, function () {
     $(this).text($(this).attr('data-percentage'));
 });

最佳答案

因为 this 不是指您的元素。您仍处于同一范围内,因此 this 不会改变。你必须这样做:

// You need the easing plugin
// https://github.com/danro/jquery-easing

$('#test1 .bar[data-percentage]').each(function() {
    var $this = $(this);

    $this.animate({
        width: $this.data('percentage')
    }, {
        duration: 1000,
        //easing: 'easeOutBounce',
        step: function(value) {
            $(this).text(value + '%');
        }
    });
});

演示:http://jsfiddle.net/qZVv4/4/

关于jQuery 此选择器不可用于 animate() 方法属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073563/

相关文章:

javascript - 如何使用 jquery 和 javascript 创建模态窗口以帮助用户做出选择

javascript - 以正确的顺序执行 jQuery 脚本

javascript - jquery 菜单滑动和更改按钮文本

javascript - 根据值更改 jquery.animate

jquery - 水平 Jquery 菜单 - 试图停止动画队列

javascript - 悬停父div时,更改子div的背景色

javascript - 替换所有字符串jquery中的制表符

javascript - 为什么在 jQuery 中使用 load() 而不是 htmls?

C++ 从其线程函数中引用一个 boost::thread

javascript - 类中的jquery函数混淆了this关键字