[英]Allow click but disable link
I have a link that I needs to be disabled based on some condition. 我有一个链接,我需要根据某些条件禁用。 So I have used CSS way of disabling it as follows:
所以我使用CSS方式禁用它如下:
.disabled {
pointer-events: none
}
if(some condition) {
jQuery("#some_link_id").addClass("disabled");
}
But, as expected it has disabled all events (like: click, hover, etc). 但是,正如预期的那样,它已禁用所有事件(例如:点击,悬停等)。 I want to put my own click event on the link if it's disabled, eg:
如果它被禁用,我想在链接上放置我自己的点击事件,例如:
jQuery("#some_link_id.disabled").click(function(){
alert("Link is disabled");
});
How can this be achieved? 怎么能实现这一目标?
Try this: 尝试这个:
$(document).ready(function() { $(".disabled").click(function() {//all elements with .disabled class return false;//disable the link }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="disabled" href="http://www.instructables.com">Link text</a>
I think you should use jQuery & no need to use css 我认为你应该使用jQuery而不需要使用css
$("a").click(function(event){
event.preventDefault();
alert("Link is now disabled");
});
If you set pointer events to none, you can't bind any events to the link. 如果将指针事件设置为none,则无法将任何事件绑定到链接。 It would be better to just dynamically change the output with a function.
使用函数动态更改输出会更好。
$('#somelink').click(function(e){
if( $(this).hasClass('disabled') ){
// Do this
} else {
// Do that
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.