modify event data during propagation

后端 未结 3 735
心在旅途
心在旅途 2020-12-05 17:06

Is there any way of attaching data to a jQuery event object as it propagates up the DOM?

To clarify, if you have three nested divs, each with a click event listener

3条回答
  •  被撕碎了的回忆
    2020-12-05 18:06

    Although you cannot attach data directly to the event, you could still attach data to the targetElement via jQuery's data():

    $('div').click(function(e) {
        var info = $(e.target).data('info') || '';
        $(e.target).data('info', info + ' ' + $(this).attr('class'));
    });
    

    Here's the fiddle: http://jsfiddle.net/eKVmU/

提交回复
热议问题