【问题标题】:How to disable a bootstrap div which uses button class如何禁用使用按钮类的引导 div
【发布时间】:2017-03-19 16:31:09
【问题描述】:

如何禁用使用按钮类的引导 div。根据某些条件使用 javascript 禁用按钮

这是我要使用 js 禁用的按钮代码:

<div class="btn-group btn-group-justified">
    <a href="#" class="btn btn-info col-sm-3">
        <i class="glyphicon glyphicon-send" ></i>
        <br>
        Send
    </a>
</div>

【问题讨论】:

  • disabled 适用于组件,例如 select、input 等元素,例如 div 和 span,您可以添加一个类并通过 css 指针事件设置 none

标签: javascript twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

div可以这样隐藏---

$('.zz').css('display', 'none');
            
   
          
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
          <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
          
  </head>

    <body>
     <div class="btn-group btn-group-justified zz" id="btn-disable">
              <a href="#" class="btn btn-info col-sm-3">
                <i class="glyphicon glyphicon-send" ></i><br>
                Send
              </a>
            </div>  
</body>
  </html>

如果你想用 onclick 事件隐藏 div 你可以这样做--

              $('.zz').click(function(){
              $('.zz').css('display', 'none');
            });
   
         
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
          <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
          
  </head>

    <body>
     <div class="btn-group btn-group-justified zz" id="btn-disable">
              <a href="#" class="btn btn-info col-sm-3" >
                <i class="glyphicon glyphicon-send" ></i><br>
                Send
              </a>
             </div> 
</body>
  </html>

现在您可以通过这种方式禁用它

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
          <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
         
  </head>

    <body>
     <div class="btn-group btn-group-justified zz" id="btn-disable">
              <a href="#" class="btn btn-info col-sm-3" >
                <i class="glyphicon glyphicon-send" ></i><br>
                Send
              </a>
              <script type="text/javascript">
              $('.zz').click(function(){
             $(".zz *").attr("disabled", "disabled").off('click');
            });
   
          </script>
       </div>
</body>
  </html>
  

因为您刚刚开始使用 js 和 xml。

This example can be helpful

现在,如果您想根据某个值禁用按钮,请执行以下操作--

注意 这只是给你一些想法的粗略方法。 在此示例中,您有两个值 hibye,它们来自您的 xml 并显示在 HTML 页面上。因此,基于该值,您将禁用 div。

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
          <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
          
  </head>

    <body>
      <div class="btn-group btn-group-justified zz2" id="btn-disable2">
              <a href="#" class="btn btn-info col-sm-3" >
                <i class="glyphicon glyphicon-send" ></i><br>
                bye
              </a>
        </div>
     <div class="btn-group btn-group-justified zz1" id="btn-disable1">
              <a href="#" class="btn btn-info col-sm-3" >
                <i class="glyphicon glyphicon-send" ></i><br>
                hi
              </a>
       </div>
              <script type="text/javascript">
                var node = document.getElementById('btn-disable2');



textContent = node.textContent;
                alert($.trim(textContent));
             if ($.trim(textContent) == 'bye') {
             $(".zz2 *").attr("disabled", "disabled").off('click');
            }
                
   
       var node1 = document.getElementById('btn-disable1');



textContent1 = node1.textContent;
                alert($.trim(textContent1));
             if ($.trim(textContent1) == 'bye') {
             $(".zz1 *").attr("disabled", "disabled").off('click');
            }
                   </script>
       
</body>
  </html>

【讨论】:

  • 1.看,我已经从 js 中的 xml 中获取了一些值,现在我希望它们在引导程序中设计的其他 html 页面中显示这些值。我是 javascript 的初学者。 2. 基于 js 文件中的某些条件,我必须禁用我在此处提到的此按钮。所以我想知道我可以显示该按钮对于某些值是禁用的。
  • @user7455856:提到我在你的问题中看不到的条件。
  • 条件只是检查是否有效用户。对于某些用户,我必须启用此按钮,而对于某些我必须禁用的用户。就是这样。
  • 如何将获取的 xml 数据从 js 文件传递​​到另一个 html 页面?
  • @user7455856:我已经编辑了我的答案,希望对你有所帮助;)
【解决方案2】:

试试这个:

  document.getElementsByClassName("btn")[0].style.pointerEvents = "none";
  var timestampArray = document.getElementsByClassName("btn");

    for(var i = (timestampArray.length - 1); i >= 0; i--)
    {
        timestampArray[i].style.pointerEvents = "none";
    }

Working fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多