使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?


当前回答

JavaScript 非常广泛。 如果您想要跳转到另一页, 您有三个选项 。

 window.location.href='otherpage.com';
 window.location.assign('otherpage.com');
 //and...

 window.location.replace('otherpage.com');

当您想要移动到另一页时, 如果这是您的要求, 您可以使用其中的任何内容 。 但是所有三种选择都局限于不同的情况 。 请按照您的要求明智地选择 。

如果您有兴趣了解更多有关这个概念的知识,可以更深入地了解。

window.location.href; // Returns the href (URL) of the current page
window.location.hostname; // Returns the domain name of the web host
window.location.pathname; // Returns the path and filename of the current page
window.location.protocol; // Returns the web protocol used (http: or https:)
window.location.assign; // Loads a new document
window.location.replace; // RReplace the current location with new one.

其他回答

但如果有人想重回主页 他可能会使用下面的片段

window.location = window.location.host

如果你有三种不同的环境,如发展、中转和生产,将会很有帮助。

您可以将这些单词放入铬控制台, 来探索此窗口或窗口。 位置对象 。 @ info: whatsthis火虫控制台

如果您想做更多描述的话, 这样做会有所帮助。 如果您试图生成页码数据, 这样做的方法中有一些选项。 您可以为您想要直接进入的每页创建单独的链接 。

<a href='/path-to-page?page=1' class='pager-link'>1</a>
<a href='/path-to-page?page=2' class='pager-link'>2</a>
<span class='pager-link current-page'>3</a>
...

请注意,此示例中的当前页面在代码中和与 CSS 的处理方式不同。

如果您想要通过 AJAX 更改页码数据, 则这里是 jQuery 会出现的位置。 您要做的是将点击处理器添加到与不同页面相对应的每个锁定标记中。 此点击处理器会引用一些 jQuery 代码, 该代码会通过 AJAX 去取取下一页, 并用新数据更新表格 。 下面的例子假设您有一个返回新页面数据的网络服务 。

$(document).ready( function() {
    $('a.pager-link').click( function() {
        var page = $(this).attr('href').split(/\?/)[1];
        $.ajax({
            type: 'POST',
            url: '/path-to-service',
            data: page,
            success: function(content) {
               $('#myTable').html(content);  // replace
            }
        });
        return false; // to stop link
    });
});

尝试此 :

location.assign("http://www.google.com");

示例代码片断.

标注描述 :

window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');

杰克里:

var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window

使用 :

function redirect(a) {
    location = a
}

并称之为:redirect([url]);

没必要href之后location,因为它是隐含的。