我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
在JavaScript中,您可以通过使用以下方式重新引导到一个特定的页面:
window.location.replace("http://www.test.com");
或
location.replace("http://www.test.com");
或
window.location.href = "http://www.test.com";
使用 jQuery:
$(window).attr("location","http://www.test.com");
其他回答
与 jQuery 合作:
$(window).attr("location", "http://google.fr");
试试这:
location.assign("http://www.google.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/")
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);
var url = 'asdf.html';
window.location.href = url;