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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?