如何启用/禁用锚标记 [英] How to enable/disable Anchor Tag

查看:91
本文介绍了如何启用/禁用锚标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例适用于启用/禁用href,但不适用于 onclick .我需要为这两个属性启用/禁用

Below example is working for enable/disable of href but not for onclick. I need to enable/disable for both attributes

注意:启用后,我无法根据需要简单地将onclick事件删除/绑定到虚拟function().

Note: I cant simply remove/bind the onclick event to dummy function() as it need once we enable it.

脚本代码:

<script type="text/javascript">
 $(document).ready(function () {
     $("#b1").click(function () {
         $("#yahoo").attr("disabled", "disabled");
         $("#yahoo").css("background-color", "silver");
     })

     $("#b2").click(function () {
         $("#yahoo").removeAttr("disabled");
         $("#yahoo").css("background-color", "white");
     })

     $("#yahoo").click(function (e) {
         if ($("#yahoo").attr("disabled") == "disabled") {
             e.preventDefault();
         }
     });
 });
</script>

HTML代码:

 <div>
   <input type="button" id="b1" value="Disable Yahoo Link"> 
   <input type="button" id="b2" value="Enable Yahoo Link">    
 </div>    
 <a id="yahoo" target="_blank" href="javascript:alert('href alert')" onclick="javascript:alert('onclick alert')">Yahoo.com</a>

工作示例 http://jsfiddle.net/nunnakirankumar/suYe4/

推荐答案

在click()函数中,您需要显式返回false(发现它被禁用后).否则,默认处理程序将导致浏览器转到或运行指定的href.

Inside your click() function, you need to explicitly return false (after discovering it's disabled). Otherwise the default handler will cause the browser to go to or run the designated href.

OP很可能会继续前进,所以这个答案实际上只是为了Google搜索者的缘故.

The OP has most likely moved on, so this answer is really just for google searchers' sake.

这篇关于如何启用/禁用锚标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆