使用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.

其他回答

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

<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
    });
});

我们不能简单地使用 jQuery 重置

j/ 查询没有必要,window.location.replace(...)最好模拟 HTTP 重定向 。

window.location.replace(...)优于使用window.location.href,因为replace()用户不会陷入永无止境的后端故障中。

如果您想要模拟某人点击链接, 请使用location.href

如果您想要模拟 HTTP 重定向,请使用location.replace

例如:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

在 JavaScript 和 jQuery 中, 我们使用以下代码重定向页面 :

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

但您可以在 jQuery 中设定函数, 以重定向页面 :

jQuery.fn.redirect=function(url)
{
    window.location.href=url;
}

调用此函数 :

jQuery(window).redirect("http://stackoverflow.com/")

您可以在以下代码中使用它,getRequestToForwardPage是请求映射( 即请求映射) 。URL URL URL URL 網址)您也可以使用您的 URL。

function savePopUp(){
    $.blockUI();
    $.ajax({
        url:"GuestHouseProcessor?roomType="+$("#roomType").val(),
        data: $("#popForm").serialize(),
        dataType: "json",
        error: (function() {
            alert("Server Error");
            $.unblockUI();
    }),
    success: function(map) {
        $("#layer1").hide();
        $.unblockUI();
        window.location = "getRequestToForwardPage";
    }
});

这是针对申请的相同背景。

如果您想要只使用 jquery 特定代码, 跟随以下代码可能会有帮助 :

 $(location).attr('href',"http://www.google.com");
 $jq(window).attr("location","http://www.google.com");
 $(location).prop('href',"http://www.google.com"); 
var url = 'asdf.html';
window.location.href = url;