有人可以向我解释一下为什么“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 + '%');
}
});
});
关于jQuery 此选择器不可用于 animate() 方法属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073563/