我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?


当前回答

jQuery 代码重新引导页面或 URL

您甚至可以直接将 URL 转移到 attr() 方法,而不是使用变量。

第二条路

 window.location.href="http://stackoverflow.com";

你也可以编码如下(两者都是内部相同的):

window.location="http://stackoverflow.com";

第三种方式

第四条路

Location.assign() 方法将新文档上传到浏览器窗口中。

window.location.assign("http://stackoverflow.com"); 

第五条路

如 attr() 方法( jQuery 1.6 引入后)

var url = “http://stackoverflow.com”; $(位置)。prop('href', url);

其他回答

此分類上一篇: 你可以將延遲時間設定到你想要的任何地方:

<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Your Document Title</title>
    <script type="text/javascript">
        function delayer(delay) {
            onLoad = setTimeout('window.location.href = "http://www.google.com/"', delay);
        }
    </script>
</head>

<body>
    <script>
        delayer(8000)
    </script>
    <div>You will be redirected in 8 seconds!</div>
</body>

</html>

在JavaScript和jQuery中,我们可以使用以下代码将一页转向另一页:

window.location.href="http://google.com";
window.location.replace("page1.html");

您可以使用下面的方法重新引导页面:

使用头部中的 meta 标签 - <meta http-equiv="refresh" content="0;url=http://your-page-url.com" />. 请注意,内容="0;... 使用了几秒钟后你需要重新引导页面 使用 JavaScript: window.location.href = "http://your-page-url.com"; 使用 jQuery: $(location).attr('href', 'http://yourPage.com/');

您需要在代码中插入此行:

$(location).attr("href","http://stackoverflow.com");

如果您没有 jQuery,请使用 JavaScript:

window.location.replace("http://stackoverflow.com");
window.location.href("http://stackoverflow.com");

location.assign(): 指定路径通过路径到它.. 指定将给你一个历史,即使路径被分配后. 使用方法:值应该转移到它. 例如: location.assign(“http://google.com”) location.href 可以定义给一个路径到它... 它将重定向到一个特定的路径一旦它设置,它将保持历史... 使用方法:值应该分配到它


assign() 和 href 是相似的,两者都可以持有历史。 assign 将通过通过值工作, href 通过分配工作。

您可以使用 JavaScript 本身实现,而不使用 jQuery 通过分配,

window.location = "http://google.com"
location.href = "http://google.com"

你可以得到类似的事情使用jQuery如下。

$(window).attr('location', "http://www.google.com");
$(location).attr('href', "http://www.google.com");

你可以很容易地理解两者之间的差异。

这里是位置对象,

此分類上一篇