admin管理员组文章数量:1203199
I want to make an additional click handler (client page, cant modify his js/html) and it should work like this in my script:
1) event.stopPropagation
(pause client click propagation)
2) my function (do my function, when everything is done do next)
3) event.startPropagation
(continue standard client action)
Right now, the first and second work. The third is the problem.
I know event.startPropagation
doesn't exist, but I want something like that.
Any hints?
I want to make an additional click handler (client page, cant modify his js/html) and it should work like this in my script:
1) event.stopPropagation
(pause client click propagation)
2) my function (do my function, when everything is done do next)
3) event.startPropagation
(continue standard client action)
Right now, the first and second work. The third is the problem.
I know event.startPropagation
doesn't exist, but I want something like that.
Any hints?
3 Answers
Reset to default 10You can re-trigger the same event object on the parent node, f.ex (jQuery). You will need to copy the event object first and pass it into the trigger in order to capture the same event properties in the bubble (f.ex e.pageX
):
var copy = $.extend(true, {}, e);
setTimeout(function() {
$(copy.target.parentNode).trigger(copy);
},500);
e.stopPropagation();
Demo: http://jsfiddle.net/GKkth/
EDIT
Based on your comment, I think you are looking for something like:
$.fn.bindFirst = function(type, handler) {
return this.each(function() {
var elm = this;
var evs = $._data(this).events;
if ( type in evs ) {
var handlers = evs[type].map(function(ev) {
return ev.handler;
});
$(elm).unbind(type).on(type, function(e) {
handler.call(elm, e, function() {
handlers.forEach(function(fn) {
fn.call(elm);
});
});
});
}
});
};
This prototype allows you to bind a "premium handler" that holds a next
function that will execute all previous handlers to the same element when you want to. Use it like:
$('button').click(function() {
console.log('first');
}).click(function() {
console.log('second');
}).bindFirst('click', function(e, next) {
console.log('do something first');
setTimeout(next, 1000); // holds the other handlers for 1sec
});
DEMO: http://jsfiddle.net/BGuU3/1/
Don't stop propagation in the first place.
Test if my function
does what you want it to do, and then decide if you are going to stop propagation or not.
Add and remove the click handler if you want to enable/disable the click event respectively. Because event.stopPropagation()
will stop the events from propagating up the dom tree.
Update:
If you are using jQuery, use .trigger()
function.
http://api.jquery.com/trigger/
本文标签: javascriptstopPropagation and startPropagationStack Overflow
版权声明:本文标题:javascript - stopPropagation and startPropagation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738665684a2105686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论