2

如何覆盖aFunction内部 child.js

我希望子事件监听器在不触发的情况下aFunction触发,我无法修改parent.js.

这甚至可能吗?

索引.html:

       <th class="foo">foo &nbsp;&nbsp;

            <span class="bar" data-feather="arrow-down"></span>

        </th>

父.js:

       parent = {
            init: function() {
                parent.aFunction($('.foo'));
            },
            aFunction: function(element) {
                element.on('click', function() {
                    alert('foo');
                });
            }, ...

         window.onload = parent.init;

child.js:

    $('.bar').on('click', function() {
      alert('bar');
    });
4

1 回答 1

4
  • 首先它的.bar原因是没有调用 html 标签bar
  • event.stopPropagation() 防止事件在 DOM 树中冒泡,防止任何父处理程序收到事件通知。

例子

$('.foo').on('click', function() {
  alert('foo');
});
$('.bar').on('click', function(e) {
  e.stopPropagation();
  alert('bar');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="foo">foo &nbsp;&nbsp;
    <span class="bar" data-feather="arrow-down">Bar</span>
</div>

于 2019-02-13T02:38:13.753 回答