简体   繁体   English

如何在有事件的链接中获取数据?

[英]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 第一传递当前元素的引用作为thisga方法

<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 现在在ga()方法中,您可以执行

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. 如果要访问链接的data-属性值(在data-id="161795"data-click="view-seller-contact" ),请使用.data()方法。

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

The linkData variable will be an object - { id: 161795, click: "view-seller-contact" } . linkData变量将是一个对象- { id: 161795, click: "view-seller-contact" } Use it how you want ) 随心所欲地使用它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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