点击使用jQuery后更改链接的href [英] Changing a links href after click with jQuery

查看:215
本文介绍了点击使用jQuery后更改链接的href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个链接,点击时,切换其href属性,然后进入到该位置。

I am trying to create a link that when clicked on, switches its href attribute, and then goes to that location.

我的HTML是:

<a href="http://google.com" rel="group" data-wpurl="http://yahoo.com"></a>

当点击时,我想浏览器访问到数据wpurl位置,而不是HREF位置。我使用的是数据属性的原因是因为我使用的应用程序需要使用HREF的......不是与此有关。

When clicked, I would like the browser to go to the data-wpurl location, not href location. The reason I am using a data attribute is because of the application I am using requires use of the href...not relevant here.

我的jQuery是:

$('a[rel="group"]').on('click', function(e) {
      e.preventDefault();
      var wpurl = $(this).attr("data-wpurl");
      $(this).attr('href', wpurl);
});

我使用e preventDefault()。到prevent采取用户在href浏览器。该数据属性分配给在href后,我怎么那么触发点击?使用触发('点击')点击();!不工作

任何想法?

推荐答案

这将是容易只是立即改变位置:

It would be easier to just change the location immediately:

e.preventDefault();
location.href = $(this).data('wpurl');

这篇关于点击使用jQuery后更改链接的href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆