[英]How to override a parent event listener when a child element is clicked in Javascript?
How do I override aFunction
inside of child.js
? 如何覆盖
aFunction
内部的child.js
?
I want the child event listener to trigger without aFunction
triggering, and I can't modify parent.js
. 我希望子事件侦听器在没有
aFunction
触发的情况aFunction
触发,并且我无法修改parent.js
。
Is this even possible? 这有可能吗?
<th class="foo">foo
<span class="bar" data-feather="arrow-down"></span>
</th>
parent = {
init: function() {
parent.aFunction($('.foo'));
},
aFunction: function(element) {
element.on('click', function() {
alert('foo');
});
}, ...
window.onload = parent.init;
child.js: child.js:
$('.bar').on('click', function() {
alert('bar');
});
.bar
cause no html tags called bar
.bar
不会导致称为bar
html标签 event.stopPropagation()
Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. event.stopPropagation()
防止事件使DOM树冒泡,从而防止任何父处理程序收到该事件的通知。 Example 例
$('.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 <span class="bar" data-feather="arrow-down">Bar</span> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.