首页 > 解决方案 > 点击时禁用和启用标签

问题描述

我们可以禁用a标签并在添加类后启用它吗?像这样的东西:

$('a').on('click', function(e) {

  e.preventDefault();
  e.stopPropagation();
  $(this).addClass('ajax-link').attr('rel', '#content')
  $(this).animate({
    'top': '100px',
  })
  // want to re-enable it and do the normal behaviour
  $(this).trigger('click')
  return true
})

标签: javascriptjquery

解决方案


你可以这样做:

$('a').on('click', function(e) {

  e.preventDefault();
  e.stopPropagation();

  $(this).addClass('ajax-link')

  window.location.href = $(this).attr('href')
})
.ajax-link {
  color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<a href="https://google.com">Test</a>

您基本上阻止了锚点的默认操作,因为单击不起作用,然后您添加类并手动将用户发送到链接。

我相信还有很多其他方法可以按照您的要求进行操作,这只是其中一种简单直观的方法。


推荐阅读