当事件发生时,会通过名称触发另一个事件。在某些情况下,第二个处理程序可以调用 `preventDefault`。如何将此调用传递给原始事件?
https://jsfiddle.net/edgv8qur/1/
$("button").click(function (event) {
$("body").trigger("some-custom-event");
// If the custom event was prevented, prevent original one too
if (true) { // How to check?
event.preventDefault();
}
})
$("body").on("some-custom-event", function (event) {
if (Math.random() > .5) {
console.log("prevent");
event.preventDefault();
} else {
console.log("allow");
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button>Test</button>
.smth
这样的调度根本不起作用,甚至不能传递给事件。 - Qwertiy