使用preventDefault方法删除后,如何继续使用click事件? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我有一个链接,我用.preventDefault方法取消“点击”事件,以检查用户是否登录。如果用户使用正确的角色登录,我想继续链接后的点击效果,回家?

jQuery('.btn-read-more').on('click', function(e) {
    e.preventDefault();
    console.log(e.currentTarget);
    var data = {
        action: 'is_user_logged_in'
    };

    jQuery.post(ajaxurl, data, function(response) {
        console.log(response);
        if (response === 'no') {
            jQuery('#modal_login_form_div').modal('show');
        } else if (response === 'ccwdpo_user' || response === 'ccwdpo_customer') {
            //e.currentTarget.click();
            //console.log(e.currentTarget);
            window.location = jQuery(this).attr("href");    
        }
    });
});

现在,我解决了:

jQuery('.permalink').on('click', function(e) {
    e.preventDefault();
    var data = {
        action: 'is_user_logged_in'
    };

    var permalink = jQuery(this).attr('href');
    //console.log(permalink);
    jQuery.post(ajaxurl, data, function(response) {
        console.log(response);
        if (response === 'no') {
            jQuery('#modal_login_form_div').modal('show');
        } else if (response === 'ccwdpo_user' || response === 'ccwdpo_customer') {
            window.location = permalink;
        } else {
            jQuery('#modal_error_div').modal('show');
        }
    });
});
javascript jquery events onclick preventdefault
1个回答
0
投票

这将做你想要的。从事件中获取href值并设置window.location

document.querySelector('a').onclick = ev => {
  // Prevent default
  ev.preventDefault();
  // Continue with action (note this does not handle if other attributes, such as `target="_blank"` is set)
  window.location = ev.target.getAttribute('href');
};
<a href="https://stackoverflow.com">Go to google</a>
© www.soinside.com 2019 - 2024. All rights reserved.