如何通过JavaScript导航锚标记中的href [英] How to navigate href in anchor tag via JavaScript

查看:68
本文介绍了如何通过JavaScript导航锚标记中的href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法让JavaScript模仿用户点击页面上的锚标记?这意味着需要设置Referrer Url。仅设置document.location.href不会设置引荐来源网址。

Is there an easy way to have JavaScript mimic a User clicking an anchor tag on a page? That means the Referrer Url needs to be set. Just setting the document.location.href doesn't set the Referrer Url.

<script>
  $(document).ready(function () {
    $("a").click();
});
</script>

<a href="http://example.com">Go here</a>

这不起作用,因为链接没有Click()事件设置。

This doesn't work because there isn't a Click() event setup for the link.

推荐答案

你可以这样做:

window.location = $("a").attr("href");

如果你想保留推荐人,你可以这样做:

If you want to keep the referrer, you could do this:

var href = $('a').attr('href');
$('<form>').attr({action: href, method: 'GET'}).appendTo($('body')).submit();

这是hackish,但适用于所有浏览器。

It is hackish, but works in all browsers.

这篇关于如何通过JavaScript导航锚标记中的href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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