wild84 2017-09-11 08:53 采纳率: 80%
浏览 2297
已采纳

通过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条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波