jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – ajax请求后的HTML Anchor标记重定向链接大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试编写一种方法来捕获网站菜单上的用户点击,但我希望该过程对用户保持沉,主要是当用户将鼠标悬停在锚标记上时,它会表现正常(显示链接)包含).我已经尝试过各种方式来做到这一点,我现在几乎都在使用它,我的代码如下:

<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); ">
    Google
  </a>
  <img class="childImage" src="icon.png">
</div>

在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;
      }
    });
});

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

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

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

<a href="javascript:saveClick(o)">Google</a>

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

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

解决方法

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

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

    $.ajax({
      type: 'POST',complete: function(data){
        console.log("DONE");
        LOCATIOn.href = self.href;
      }
    });
});

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

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

    $.ajax({
      type: 'POST',context: this,complete: function(data){
        console.log("DONE");
        LOCATIOn.href = this.href;
      }
    });
});

大佬总结

以上是大佬教程为你收集整理的jquery – ajax请求后的HTML Anchor标记重定向链接全部内容,希望文章能够帮你解决jquery – ajax请求后的HTML Anchor标记重定向链接所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。