javascript - 点击时禁用和启用标签
问题描述
我们可以禁用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
})
解决方案
你可以这样做:
$('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>
您基本上阻止了锚点的默认操作,因为单击不起作用,然后您添加类并手动将用户发送到链接。
我相信还有很多其他方法可以按照您的要求进行操作,这只是其中一种简单直观的方法。
推荐阅读
- javascript - 如何在javascript函数中提交表单?
- perl - 用两个数组做一个散列,一个散列有一个数组值的键
- java - JTable 只在一列中显示所有记录(数据)?
- mongodb - 限制从特定 json 参数返回的结果
- nginx - Nginx 域名解析问题
- sequelize.js - 如何导入 sequelize js 以执行原始查询
- c++ - “Hello World”程序在 Visual Studio 更新后的 Windows PE 上不起作用
- ios - 停止“MPMusicPlayerController.systemMusicPlayer”曲目后,Spotify 曲目未播放
- angularjs - AngularJs,在ui-grid中显示函数结果
- html - 保持按钮固定,但在体内