Bootstrap设置按钮禁用

在Bootstrap中,按钮可以使用button标签或者a标签。设置按钮禁用可以通过两种方式,一种是通用CSS样式,一种是用过JS脚本动态设置,下面举例说明!

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Bootstrap设置按钮禁用</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">  
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $('#btn1').addClass('disabled'); //禁用 --变灰,依然可以调用点击事件
            $('#btn1').click(function() {
                alert('新增,复活了!');
            });

            $('#btn2').prop('disabled', true); //禁用 --变灰,且不能调用点击事件
            $('#btn2').click(function() {
                alert('删除,复活了!');
            });

            $('#btn3').click(function() {
                $('#btn1').removeClass('disabled'); //启用
            });

            $('#btn4').click(function() {
                $('#btn2').prop('disabled', false); //启用
            })
        });
    </script>
</head>
<body>
    
<div class="container">
    <h2>按钮</h2>
    <p> .btn-default 类是默认/标准按钮样式:</p>
    <!-- 方法一:CSS样式 -->
    <!-- <button type="button" class="btn btn-default disabled">默认</button> -->

    <!-- 方法二:JS脚本 -->
    <button id="btn1" type="button" class="btn btn-default">新增</button>
    <button id="btn2" type="button" class="btn btn-default">删除</button>
    <button id="btn3" type="button" class="btn btn-default">启用新增</button>
    <button id="btn4" type="button" class="btn btn-default">启用删除</button>
</div>
    
</body>
</html>


 

原文地址:https://www.cnblogs.com/xiaxue168/p/10527993.html