【问题标题】:Allow click but disable link允许点击但禁用链接
【发布时间】:2015-11-25 05:43:23
【问题描述】:

我有一个链接,我需要根据某些情况禁用它。所以我使用 CSS 的方式来禁用它,如下所示:

.disabled {
  pointer-events: none
}

if(some condition) {
  jQuery("#some_link_id").addClass("disabled");
}

但是,正如预期的那样,它已禁用所有事件(例如:单击、悬停等)。如果链接被禁用,我想在链接上放置我自己的点击事件,例如:

jQuery("#some_link_id.disabled").click(function(){
  alert("Link is disabled");
});

如何做到这一点?

【问题讨论】:

  • 使用 CSS pointer-events:none;
  • @www139 我有,请看问题
  • 对不起(哈哈——我的错)
  • 我写了我的答案,希望对你有帮助:)

标签: javascript jquery html css


【解决方案1】:

我认为你应该使用 jQuery 而无需使用 css

$("a").click(function(event){
    event.preventDefault();
    alert("Link is now disabled");
});

【讨论】:

    【解决方案2】:

    试试这个:

    $(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>

    【讨论】:

      【解决方案3】:

      如果将指针事件设置为无,则无法将任何事件绑定到链接。最好只使用函数动态更改输出。

      $('#somelink').click(function(e){
             if( $(this).hasClass('disabled') ){
                  // Do this
             } else {
                  // Do that
             }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-23
        • 2014-06-05
        • 1970-01-01
        • 1970-01-01
        • 2012-09-19
        • 1970-01-01
        相关资源
        最近更新 更多