最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get at target element with jQuery event listener? - Stack Overflow

programmeradmin2浏览0评论

I have an event listener, and an element that triggers a custom event on click. They’re set up as follows:

$(document).on('customEventName', function(e) {
    // do something
});

$(document).on('click', '[data-action]', function(e) {
    e.preventDefault();
    $(document).trigger( $(this).attr('action') );
});

Say I have an anchor tag (<a href="#" data-target="customEventName">) trigger the click event, how would I then get at that <a> tag and its properties in my listener? I’m wanting to get at the object to parse any additional data- attributes.

I have an event listener, and an element that triggers a custom event on click. They’re set up as follows:

$(document).on('customEventName', function(e) {
    // do something
});

$(document).on('click', '[data-action]', function(e) {
    e.preventDefault();
    $(document).trigger( $(this).attr('action') );
});

Say I have an anchor tag (<a href="#" data-target="customEventName">) trigger the click event, how would I then get at that <a> tag and its properties in my listener? I’m wanting to get at the object to parse any additional data- attributes.

Share asked Aug 1, 2013 at 15:32 Martin BeanMartin Bean 39.4k28 gold badges132 silver badges206 bronze badges 1
  • Little confused, seems you're already using this, you want to pass the instance of this onto your custom listener? – tymeJV Commented Aug 1, 2013 at 15:36
Add a ment  | 

2 Answers 2

Reset to default 5

Solution 1: extraParameters

Use extraParamters parameter as second argument of trigger jQuery function.

$(document).on('customEventName', function(e, dataActionElement) {
    // do something
});

$(document).on('click', '[data-action]', function(e) {
    e.preventDefault();
    $(document).trigger($(this).attr('action'), [$(this)]);
});

From documentation:

.trigger( event [, extraParameters ] )

  • event

    Type: Event

    A jQuery.Event object.

  • extraParameters

    Type: Array or PlainObject

    Additional parameters to pass along to the event handler.

JSFIDDLE


Solution 2: custom events

According to documentation, you also can create a custom Event where you can set the target:

Category: Event Object

jQuery’s event system normalizes the event object according to W3C standards. The event object is guaranteed to be passed to the event handler. Most properties from the original event are copied over and normalized to the new event object.

$(document).on('customEventName', function(e) {
    // e.target is clicked element sent using customEvent
    // do something
});

$(document).on('click', '[data-action]', function(e) {
    var customEvent = $.Event($(this).attr('action'), {target: this }) 
    $(document).trigger(customEvent);
});

JSFIDDLE

You could pass it as a parameter in trigger:

$(document).on('customEventName', function(e, target) {
    // do something
});

$(document).on('click', '[data-action]', function(e) {
    e.preventDefault();
    $(document).trigger( $(this).attr('action'), [this] );
});

you could also try something like this:

$(document).on('customEventName', function(e) {
    var target = e.target;
    // do something
});

$(document).on('click', '[data-action]', function(e) {
    e.preventDefault();
    var customEvent = $.Event($(this).data('action'), { target: this });
    $(document).trigger( customEvent );
});
发布评论

评论列表(0)

  1. 暂无评论