append追加元素后click失效
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> </head> <body> <ul class="type_list" id="type_list_1"> <li class="active">全部</li> </ul> <span class="test">测试</span> </body> <script> $(function() { $('#type_list_1 li').click(function(event) { console.log('123'); }); $('#type_list_1 li').on('click', function() { console.log('456'); });
//追加的元素用 此方法可用
$(document).on('click', '#type_list_1 li', function() { console.log('789'); }); $('.test').click(function() { $('#type_list_1').append('<li>歌剧</li>'); }); }); </script> </html>
希望广大博友给予建议和指导