First of all, this problem originates from the fact the
HTML
anchor cannot be set todisabled
.
I have a lot of anchors (the <a>
tag) in my site that serves as button, lists, menu items, etc. They cannot be disabled but under some circumstances, I need to disable them. To make them disabled I'm planning to do the following:
Temporarily remove all it onclick
events and reattach it after. I plan to do this by:
onclick
handlers and saving it to an Array
of functions()
to be retrieved later, in case you want to enable it again later. (I don't know how to do this yet!)onclick
listenersI'm asking a way to retrieve only handlers for onclick events, but all I see is about retrieving all types of attached events.
EDIT : Guys, I appreciate the answers on how to disable anchors... but [you know, ] that is not my question. Some of the solutions are not possible on my scenario (too complicated to discuss here), and the solution above is currently the easiest thing that I can do. What I really want to know is how to perform something like this:
var handlers = []
handlers = $("#anchor").getClickEvents()
答案 0 :(得分:2)
如果你在onClick事件上返回false,那么它将取消该事件。如果返回true,它将正常进行。如果您使用的是jQuery,可以使用以下代码段:
jQuery的:
$('a.something').click(function (e) {
//do nothing
e.preventDefault();
});
的javascript:
function clickLink(){
if(condition)
return false;
}
答案 1 :(得分:2)
您可以尝试这样的事情:
$('body').on('click', 'a:not(.disabled)', function(){
console.log($(this).text() + ' was clicked');
}).on('click', 'a.disabled', function(e){
e.preventDefault();
});
$('a').trigger('click');
$('a.disabled').removeClass('disabled');
$('a').trigger('click');
将.on
事件分配给主父,以便考虑任何更改。
演示中的HTML:
<a href="#">1</a>
<a href="#">2</a>
<a href="#">3</a>
<a href="#" class="disabled">4</a>