本当に申し訳ございません。
昨日関連する質問をさせていただき、下記のようにtargetのid情報を拾えば動的に生成した要素にもイベントを設定できると思ったのですが、delete後にファイルをアップロードしても、
console.log("hello");が発生いたしません。
どの点に問題があるのでしょうか。。
HTML
1<html> 2 3<head> 4 <meta charset="utf-8"> 5 <title>sample</title> 6 <link rel="stylesheet" type="text/css" href="BB.css"> 7 <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> 8 9 10</head> 11<body> 12<span id="wrap"> 13 <input id="filing" type="file" name="" value=""> 14</span> 15 16<button type="button" name="button" id="delete" style="border-style: none;">delete</button> 17 18 19<script type="text/javascript"> 20document.getElementById('filing').addEventListener('change', function (evt) { 21if (evt.target.id==="filing") { 22console.log("hello"); 23} 24}); 25 26 27 28$(function(){ 29$('#delete').on('click',function(){ 30$('#filing').remove(); 31$('#wrap').append('<input id="filing" type="file" name="" value="">'); 32}) 33}) 34</script> 35</body> 36 37</html>
[追記]
HTML
1<button id="btn_1">btn</button> 2 3<script type="text/javascript"> 4function adding() { 5var elm_add = document.createElement('button'); 6elm_add.id = "hoge"; 7elm_add.textContent='btn added'; 8var objBody = document.getElementsByTagName("body").item(0); 9objBody.appendChild(elm_add); 10} 11var elm = document.getElementById("btn_1"); 12elm.addEventListener("click", adding, false); 13 14</script> 15<script type="text/javascript"> 16// var elm_2 = document.getElementById("hoge"); 17document.addEventListener("click", function(e){ 18console.log(e); 19if (e.target.id==='hoge') { 20alert('it works! congraturations!!') 21} 22}); 23</script> 24</body>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/16 15:38
退会済みユーザー
2017/10/16 15:42 編集
2017/10/16 15:48
2017/10/16 16:01
退会済みユーザー
2017/10/16 16:03 編集
2017/10/16 16:09
退会済みユーザー
2017/10/16 16:11
2017/10/16 16:25
2017/10/16 16:26