Rei*_*ica 4 javascript jquery overriding
我如何覆盖aFunction里面 child.js?
我希望子事件侦听器在不触发的情况下触发aFunction,并且无法修改parent.js。
这可能吗?
<th class="foo">foo
<span class="bar" data-feather="arrow-down"></span>
</th>
Run Code Online (Sandbox Code Playgroud)
parent = {
init: function() {
parent.aFunction($('.foo'));
},
aFunction: function(element) {
element.on('click', function() {
alert('foo');
});
}, ...
window.onload = parent.init;
Run Code Online (Sandbox Code Playgroud)
孩子.js:
$('.bar').on('click', function() {
alert('bar');
});
Run Code Online (Sandbox Code Playgroud)
.bar原因是没有调用 html 标签bar event.stopPropagation() 防止事件在 DOM 树中冒泡,从而防止任何父处理程序收到该事件的通知。例子
$('.foo').on('click', function() {
alert('foo');
});
$('.bar').on('click', function(e) {
e.stopPropagation();
alert('bar');
});Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="foo">foo
<span class="bar" data-feather="arrow-down">Bar</span>
</div>Run Code Online (Sandbox Code Playgroud)