我想知道是否有一种方法可以在父元素上使用 .children,然后使用 $(this) 通过事件处理程序仅引用特定的子元素。像这样:或在这里查看笔:http://codepen.io/coralsea/pen/JoRrRG
<div class="petals">
<div class="petal-1">
</div>
<div class="petal-2">
</div>
<div class="petal-3">
</div>
<div class="petal-4">
</div>
<div class="petal-5">
</div>
<div class="petal-6">
</div>
<div class="petal-7">
</div>
</div>
/
$(document).ready(function() {
$('.petals').children().draggable()
$(this).mouseup(function() {
$(this).animate({
backgroundColor: 'red',
top: "+=50"
}, 2000, function() {
});
});
});
我可以通过为每个 child 添加一个类(class)并针对该类(class)进行工作,如下所示:http://codepen.io/coralsea/pen/emdGmo 但似乎能够通过针对 parent 的 child 来做到这一点会更有效。
最佳答案
使用 jQuery Ui 的 stop
事件执行此操作的其他方法:
$(document).ready(function () {
$('.petals').children().draggable({
stop: function (event, ui) {
$(this).animate({
backgroundColor: 'red',
top: "+=50"
}, 2000, function () {
// Animation complete.
});
}
});
});
<强> CodePen link
关于javascript - 如何将 $(this) 应用于单个子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650830/