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


当前回答

单页申请,在同一申请路线内

window.location.pathname = '/stack';

JavaScript 的内容:

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

jQuery:

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

角质4

import { Router } from '@angular/router';
export class NavtabComponent{
    constructor(private router: Router) {
    }
    this.router.navigate(['bookings/taxi']);
}

其他回答

您可以写下按钮点击事件的 URL.Action 在脚本部分如下。

function onclick() {
    location.href = '@Url.Action("Index", "Home")';
}

使用 jQuery/JavaScript 方法重定向 HTML 页面

试试这个例子代码:

function YourJavaScriptFunction()
{
    var i = $('#login').val();
    if (i == 'login')
        window.location = "Login.php";
    else
        window.location = "Logout.php";
}

如果您想要提供一个完整的URL,如 window.location = “www.google.co.in”;

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

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

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

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

您可以使用 jQuery 如下:

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

如果你只想要jQuery,那么你可以这样做:

$jq(window).attr("location","http://yourdomain.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/")