小编jef*_*ack的帖子

为什么stopPropagation在回调中采用事件参数而不是使用'this'?

我不明白为什么,当使用stopPropagation()stopDefault()使用jQuery时,你必须在事件处理函数中向回调引入一个参数.浏览器如何知道传递给该函数的内容?另外,为什么不使用this工作?

这是有效的代码.我用粗体/星号表示令人困惑的部分:

$(document).ready(function() {
   $(".see-photos").on("click", function(**event**) {
    event.stopPropagation();
    $(this).closest(".tour").find(".photos").slideToggle();
  });
  $(".tour").on("click", function() {
    alert("This should not be called");
  });
});
Run Code Online (Sandbox Code Playgroud)

对我来说,这样会更有意义.请注意event,处理函数中的回调没有参数.

$(document).ready(function() {
  $(".see-photos").on("click", function() {
    $(this).stopPropagation();
    $(this).closest(".tour").find(".photos").slideToggle();
  });
  $(".tour").on("click", function() {
    alert("This should not be called");
  });
});
Run Code Online (Sandbox Code Playgroud)

javascript jquery stoppropagation

2
推荐指数
1
解决办法
518
查看次数

标签 统计

javascript ×1

jquery ×1

stoppropagation ×1