Populate _link() target url with href value using jquery

时光总嘲笑我的痴心妄想 提交于 2019-12-23 19:24:01

问题


How can I get the value of the href attribute in a link to populate the _link() target url value using jquery?

This is the normal code:

<a href="http://www.mydomain.com/gallery/" onclick="_gaq.push(['_link', 'http://www.mydomain.com/gallery']); return false;">Gallery</a>

There are many links that need to be updated in this fashion.


回答1:


You can create a class for the links you would like to run _gaq.push for and then use jQuery live() to bind the click event to them.

Give your links a class like gaq:

<a class="gaq" href="http://www.mydomain.com/gallery/">Gallery</a>

And use jQuery live() to give all links with this class the ability to run the push function on click.

$('.gaq').live('click', function() {
    _gaq.push(['_link', $(this).attr("href")]);
    return false;
});



回答2:


i would say the easiest way is to add a class to the <a>, then access it through the id.

$('.aClass').attr('href') //gets the value of the href
$('.aClass').attr('href', 'stackoverflow.com') //sets the value of the href

$('.aClass').click(function(){
    _gaq.push(['_link', $(this).attr('href')]); 
    return false;
});

Hope that helps. You can change the value of the attr (attribute) to access or edit its value.



来源:https://stackoverflow.com/questions/4372542/populate-link-target-url-with-href-value-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!