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


当前回答

这就是我如何使用它。

   window.location.replace('yourPage.aspx');   
   // If you're on root and redirection page is also on the root

   window.location.replace(window.location.host + '/subDirectory/yourPage.aspx');

   // If you're in sub directory and redirection page is also in some other sub directory.

其他回答

您可以在 jQuery 中像这样重定向 :

$(location).attr('href', 'http://yourPage.com/');

在单击功能上,只需添加:

window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
    window.location.href = "http://www.google.com";
});

原始问题:“如何使用 jQuery 重新定向?” , 因此, 答案执行 jQuery 补充使用案例 。


重定向到 JavaScript 的页面 :

window.location.href = "/contact/";

或者,如果您需要延迟:

setTimeout(function () {
  window.location.href = "/contact/";
}, 2000);   // Time in milliseconds

jjQuery 允许您从网页中轻松选择元素。 您可以在页面中找到任何您想要的内容, 然后使用 jQuery 添加特殊效果, 对用户动作作出反应, 或者在您选中的元素内或外显示内容并隐藏内容。 所有这些任务都以知情开始如何选择元素或事件.

$('a,img').on('click',function(e){
  e.preventDefault();
  $(this).animate({
    opacity: 0 //Put some CSS animation here
  }, 500);
  setTimeout(function(){
    // OK, finished jQuery staff, let's go redirect
    window.location.href = "/contact/";
  },500);
});

想象一下有人写了一个脚本/ 插件, 包含 10000 行代码 。 如果有 jQuery , 您可以用一两行代码连接到这个代码 。

<script type="text/javascript">
var url = "https://yourdomain.com";

// IE8 and lower fix
if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
{
    var referLink = document.createElement("a");
    referLink.href = url;
    document.body.appendChild(referLink);
    referLink.click();
}

// All other browsers
else { window.location.replace(url); }
</script>

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

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