锚定标签问题-href并单击事件

Anchor tag issue - href and click event

本文关键字:单击 事件 -href 问题 标签      更新时间:2023-09-26

我有点击和href事件的锚链接。我需要先运行click事件,然后在完成事件后,它应该调用href来访问操作类。我已经更新了jsfiddle中的示例代码,就像完成点击事件一样,它应该转发到stackoverflow.com。但点击后不会转发。

请告知。

$(document).ready(function () {
    $("div.actions").append('<a id="excelExport" class="actionButton" alt="Export to Excel" title="Export to Excel" href="listexport">click me</a>');
    $('div.actions').on('click', '#excelExport', function (e) {
        e.preventDefault();
        callajax();
    });
});
function callajax() {
    jQuery.ajax({
 url : '',
     data : 
}

JSFIDDLE

由于您阻止了链接,因此需要将this.href发送到CallAjax函数,并在成功方法中执行location=href

当然,这是假设您稍后将警报更改为Ajax调用

演示

    callajax(this.href);

function callajax(href) {
    var URL = href;
    $.get("something",{"URL":URL },function(URL){
    // url is returned from server
    location.href=URL;
  });
}

function callajax(href) {
    var URL = href;
    $.get("something",{"URL":URL },function() { // reuse url passed to function
    location.href=URL;
  });
}