简体   繁体   中英

How to grab data inside a link that has event?

I want to grab some data from a like that has event like this:

<a onclick="ga('send', 'event', 'View Seller Contact', 'Alfa Romeo', '147');" style="min-width: 123px;" data-id="161795" data-click="view-seller-contact" class="btn btn-primary-blue btn-xs " href="#">View Seller Contact</a>

This link will show data after click on it. I want to grab the data that show after the link was click. I wonder how can I grab data from this kind of things? Pls anyone give me any idea.Pls guide me. Thanks in advace

you mean the attributes of the anchor tag when it is clicked?

first pass the reference of current element as this to the ga method

<a onclick="ga('send', 'event', 'View Seller Contact', 'Alfa Romeo', '147', this);" style="min-width: 123px;" data-id="161795" data-click="view-seller-contact" class="btn btn-primary-blue btn-xs " href="#">View Seller Contact</a>

now in the ga() method you can do

function ga( arg1, arg2, arg3, arg4, arg5, thisObj )
{
   $(thisObj).data('id')
   //rest of the code
}

If you want to access the data- attributes values of the link ( data-id="161795" and data-click="view-seller-contact" in your example), use the .data() method.

$('a').on('click', function(){
    var linkData = $(this).data();
});

The linkData variable will be an object - { id: 161795, click: "view-seller-contact" } . Use it how you want )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM