Javascript 没有警报页面不会通过jquery重定向

Javascript 没有警报页面不会通过jquery重定向,javascript,jquery,Javascript,Jquery,我想将我的页面重定向到我的变量中的url,但它在没有警报的情况下无法工作 如果我把警报,然后它的重定向,否则不是 $(document).ready(function(){ $("a").click(function(){ var newurl = 'http://www.xyz.com/' + $(this).attr('href'); //$(this).attr(newurl,'href'); window.location.href = newur

我想将我的页面重定向到我的变量中的url,但它在没有警报的情况下无法工作

如果我把警报,然后它的重定向,否则不是

$(document).ready(function(){
    $("a").click(function(){
    var newurl = 'http://www.xyz.com/' + $(this).attr('href');
      //$(this).attr(newurl,'href');
      window.location.href = newurl;
       alert(newurl);
    });
});
先发制人

锚定标签

<a href="includes/footer.jsp">new url</a>

preventDefault
添加到事件处理程序中,以防止在链接中跟随URL:

$(document).ready(function(){
    $("a").click(function(e){
      e.preventDefault();
      var newurl = 'http://www.xyz.com/' + $(this).attr("href");
      window.location.href = newurl;
    });
});
试试下面的方法

$(document).ready(function () {
    $("a").click(function (event) {
        var newurl = 'http://www.xyz.com/' + $(this).attr('href');
        window.location.href = newurl;
        event.preventDefault()
    });
});

您需要使用
preventDefault()
防止默认事件传播。在jquery有机会更改之前,浏览器正在重定向到
href
。通过使用此警报,您正在延迟浏览器重定向,因此它似乎可以正常工作。

您并没有阻止默认操作,该操作将跟随链接中的URL。这将优先于手动设置URL(因为默认操作稍后发生,因此会覆盖更改)。看见我不知道它为什么与警报一起工作。它可能会以某种方式取消默认操作。