简体   繁体   English

如何根据其类使包含链接的div不可点击

[英]How to make a div containing links non-clickable based on its class

These are the previous and next buttons html code what i want to do is to disable them when another button called filter is clicked and further if some conditions are satisfied 这些是上一个和下一个按钮html代码,我要执行的操作是在单击另一个名为filter的按钮时禁用它们,如果满足某些条件,则进一步禁用它们。

 <div class="page-btn" id="page-next"><a href="testlink/index/3/1"><div class="pbtn-txt" style="color: whitesmoke;">Next </div></a></div>  

 <div class="page-btn" id="page-previous"><a href="testlink/index/3/1"><div class="pbtn-txt" style="color: whitesmoke;">previous </div></a></div>  

How can I disable them based on some condition? 如何根据某些条件禁用它们? the links are generated at runtime. 链接在运行时生成。 for example 例如

$('.filter').click(function(){

                    if(condition) {
        disable next links click using class page-btn;
        disable prev links click using class page-btn;
                    } 

});
$('.filter').click(function(){
     return false;
});

jsFiddle Demo jsFiddle演示

Edit: 编辑:

If the classes change dynamically, this is a better approach that will support it: 如果类是动态变化的,那么这将是一种更好的方法来支持它:

$(document).on("click", ".filter",
    function () {
        return false;
    });

jsFiddle Demo jsFiddle演示

Edit 2: 编辑2:

After you've described your problem better, this is probably what you're looking for: 在更好地描述了问题之后,这可能就是您想要的:

$('.filter').click(function(){

      if (condition) {
          $(".page-btn a").click(
              function () { return false; }
          );
      } 
});

This code checks the class of the link when clicked: 这段代码在单击时检查链接的类:

<script type="text/javascript">
    $(function() {    
        $('.filter').on('click', function(){
            if(condition)
               $(".page-btn a").attr('href', 'javascript:void(0);');
        });
    });
</script>

Or You could use 或者您可以使用

$('.filter').click(function(e){
                if(condition) {
                  e.preventDefault();
                } 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM