我有一个链接,我使用.preventDefault方法取消了“ click”事件,以检查用户是否登录。如果该用户以正确的角色登录,我想继续使用链接后的点击效果,家吗?
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');
}
});
});
答案 0 :(得分: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>