javascript - 如何将 $(this) 应用于单个子元素

标签 javascript jquery css

我想知道是否有一种方法可以在父元素上使用 .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/

相关文章:

javascript - Jquery 添加/删除类不起作用

javascript - PhoneGap InAppBrowser URL 在 loadstart 中未定义

javascript - JSON.stringify 一个带有 Knockout JS 变量的对象

javascript - 当页面上的所有视频都已完全加载时执行 JavaScript

javascript - 简单的 jQuery "push"菜单 - 偏移主容器和菜单但转换时间不正确

jquery - 仅在较小的屏幕上切换导航

javascript - 结合 vuejs v-repeat 和 php foreach 循环?

javascript - 刷新 Autopostback 下拉列表上的 Javascript 代码

javascript - 在类中添加 attr name=name[]

javascript - 当 javascript 值插入到 .net 文本框时,document.getElementsByName(...)[0] 未定义