[英]Animate the Parent <div> when using class in jQuery
<div class="contentBlockTwo"> <span class="moreButton">+More</span> <br /> Or new flashes.</div>
As you can see I have my "spans" as the same class so I can apply the following jQuery to them: 如您所见,我将“ span”作为同一个类,因此可以将以下jQuery应用于它们:
$('.moreButton').click(function () { $('.moreButton').parent().animate({ width: '98%' }, 800); });
But no matter which of the "span" items I click, ALL of the "Divs" have the animation applied to them. 但是无论我单击哪个“ span”项目,所有“ Div”都将动画应用到它们。
How can I apply the animation to the parent of the sender/ clicked "span" without giving each "div" an ID? 如何在不给每个“ div”一个ID的情况下将动画应用于发件人的父级/单击“跨度”?
Thanks 谢谢
replace 更换
$('.moreButton').parent().animate({ width: '98%' }, 800);
with 同
$(this).parent().animate({ width: '98%' }, 800);
http://jsfiddle.net/53TjS/1/ http://jsfiddle.net/53TjS/1/
Yeah, I think that does it. 是的,我认为可以做到。
$(this).parent('div').animate(...)
$('.moreButton').click(function () {
$(this).parent().animate({ width: '98%' }, 800);
});
You need to use the this
object otherwise it is re-selecting the parent elements of all .moreButton rather than the one you clicked. 您需要使用
this
对象,否则它将重新选择所有 .moreButton的父元素,而不是单击的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.