wild84 2017-09-11 08:53 采纳率: 83.3%
浏览 2302
已采纳

通过append添加的按钮,再通过样式获取不到

代码如下:

 <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"> 
    <title>Bootstrap 实例 - 按钮标签</title>
    <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">  
    <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div>
<button class="btn btn-default" type="button">按钮</button>
</div>
    <script>
        $(".btn").click(function(){
            $("div").append('<button class="alert" type="button">按钮</button>');
        })
        $(".alert").click(function(){
            alert(1);
        })
        </script>
</body>
</html>
  • 写回答

4条回答 默认 最新

  • 珍妮的小罐君 2017-09-11 09:18
    关注
    $(".btn").click(function(){ $("div").append('<button id="alert12" class="alert" type="button">按钮</button>'); }); $('div').on('click','.alert',function(event) { alert('111'); });

    说明:用bind和click只能为页面已经加载好的dom元素绑定事件,而对于动态添加的元素,可选择用delegate()/live()/on()方法来绑定事件

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?