ajax请求后的HTML Anchor标记重定向链接

des*_*sto 5 html ajax anchor jquery preventdefault

我正在尝试编写一种方法来捕获网站菜单上的用户点击,但我希望该过程对用户保持沉默,主要是当用户将鼠标悬停在锚标记上时,它会表现正常(显示链接)包含).我已经尝试过各种方式来做到这一点,我现在几乎都在使用它,我的代码如下:

<div class="selectNode" id="selectNode_1" style="color: rgb(229, 229, 229); background-color: rgb(47, 47, 47); ">
  <a href="http://www.google.com" class="selectAnchor" style="color: rgb(229, 229, 229); ">
    Google
  </a>
  <img class="childImage" src="icon.png">
</div>
Run Code Online (Sandbox Code Playgroud)

在jQuery中,我有:

$(".selectAnchor, .menuAnchor").click(function(event){
    event.stopPropagation();
    console.log(event.target.nodeName);
    //event.preventDefault();

    $.ajax({
      type: 'POST',
      url: 'http://localsite/menuRedirect.php',
      data: {id:0, module:'Module',source:'Source'},
      complete: function(data){
        console.log("DONE");
        return true;
      }
    });
});
Run Code Online (Sandbox Code Playgroud)

链接重定向到所选页面,但ajax代码永远不会完成,因此永远不会注册用户的点击.

我尝试使用event.preventDefault,但无法恢复已取消的事件.

问题的一部分来自额外的功能,比如,大多数用户右键单击锚标签以在新选项卡中打开(或使用控制+单击,或中键单击),并使用类似的东西

<a href="javascript:saveClick(o)">Google</a>
Run Code Online (Sandbox Code Playgroud)

不允许在网站上(出于安全原因).

有什么建议可以做到这一点?

use*_*654 13

使用event.preventDefault,然后成功,使用location.href = self.href

$(".selectAnchor, .menuAnchor").click(function(event){
    event.preventDefault();
    console.log(event.target.nodeName);
    var self = this;

    $.ajax({
      type: 'POST',
      url: 'http://localsite/menuRedirect.php',
      data: {id:0, module:'Module',source:'Source'},
      complete: function(data){
        console.log("DONE");
        location.href = self.href;
      }
    });
});
Run Code Online (Sandbox Code Playgroud)

或者使用context属性来删除 var self = this

$(".selectAnchor, .menuAnchor").click(function(event){
    event.preventDefault();
    console.log(event.target.nodeName);

    $.ajax({
      type: 'POST',
      context: this,
      url: 'http://localsite/menuRedirect.php',
      data: {id:0, module:'Module',source:'Source'},
      complete: function(data){
        console.log("DONE");
        location.href = this.href;
      }
    });
});
Run Code Online (Sandbox Code Playgroud)