用jQuery点击后更改链接href [英] Changing a links href after click with jQuery
问题描述
我正在尝试创建一个链接,单击该链接时会切换其 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>
点击后,我希望浏览器转到 data-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 是:
My jQuery is:
$('a[rel="group"]').on('click', function(e) {
e.preventDefault();
var wpurl = $(this).attr("data-wpurl");
$(this).attr('href', wpurl);
});
我正在使用 e.preventDefault();以防止浏览器将用户带到 href.将 data 属性分配给 href 后,如何触发点击?使用 trigger('click')
和 click();
不起作用!
I am using e.preventDefault(); to prevent the browser from taking the user to the href. After the data attribute is assigned to the href, how do I then trigger a click? Using trigger('click')
and click();
do not work!
有什么想法吗?
推荐答案
直接更改位置会更容易:
It would be easier to just change the location immediately:
e.preventDefault();
location.href = $(this).data('wpurl');
这篇关于用jQuery点击后更改链接href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!