2016-12-04 2 views
1

JavaScriptでToDoリストを実行しています。私はボタンを使ってdivを削除します。これはJavaScriptで動的に作成されます。。すでにそのボタンにidを割り当てましたが、アクセス方法はわかりません。誰も助けることができますか? 以下の私のコード: -動的に作成されたボタンを使用してdivを削除します。

var Addnew = document.querySelector('#add_new'); 
 
var myDiv; 
 
var button; 
 

 
function newDiv() { 
 
    var body = document.getElementsByTagName('body')[0]; 
 
    myDiv = document.createElement('div'); 
 
    var userEntry = document.querySelector('.newIn').value; 
 
    button = document.createElement('button'); 
 
    myDiv.className = 'item'; 
 
    myDiv.style.display = 'block'; 
 
    myDiv.innerHTML = userEntry; 
 
    button.style.display = 'block'; 
 
    button.innerHTML = "Remove"; 
 
    button.setAttribute('id','Remove'); 
 
    button.style.marginLeft = '280px'; 
 
    button.style.background = 'tomato'; 
 
    button.style.color = 'white'; 
 
    myDiv.appendChild(button); 
 
    body.appendChild(myDiv); 
 
} 
 
document.getElementById('Remove').addEventListener('click', function() { 
 
    console.log('done'); 
 
}); 
 

 
Addnew.addEventListener('click', function() { 
 
    newDiv(); 
 
}); 
 
document.querySelector('.newIn').addEventListener('keydown', function(e) { 
 
    if (e.which == 13) { 
 
     newDiv(); 
 
    } 
 
});
.input_box{ 
 
    text-align: center; 
 
    margin-top: 7%; 
 
} 
 
.input_box > input { 
 
    width:250px; 
 
} 
 
#add_new{ 
 
    background-color:rgb(44, 147, 124); 
 
    color:white; 
 
    width:70px; 
 
    margin-left:5px; 
 
} 
 
h1{ 
 
    text-align: center; 
 
    font-family:sans-serif;; 
 
} 
 
.item{ 
 
    width:270px; 
 
    height:35px; 
 
    margin-top: 20px; 
 
    margin-left:38%; 
 
    display: none; 
 
    border: 0.5px ridge green; 
 
} 
 
#Remove { 
 
    display: none; 
 
}
<!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>To-do List</title> 
 
    <!-- Css linking --> 
 
    <link rel="stylesheet" href="styles.css"> 
 
    </head> 
 
    <body> 
 
    <h1>To-do list</h1> 
 
    <div class="input_box"> 
 
     <input type="text" class="newIn" placeholder="What do you want to add?" autofocus="autofocus"/> 
 
     <button id="add_new">Add</button> 
 
    </div> 
 
    <div class="item"></div> <button id="Remove"></button> 
 
    <script src="main.js" charset="utf-8"></script> 
 
    </body> 
 
</html>

+0

あなたは 'onclickの' イベントを使用していないでしょうか? –

+0

onclickはそれを行うもう一つの方法です。 Clickイベントが正常に機能しています.iで入力した値をdivに挿入します。 –

+0

innerHtmlではなくtext属性を使用してください。 –

答えて

0

あなたはクラスの構文で使用するクエリセレクタを持っているが、#を追加しようと予想されるとして、それが正常に動作する必要があり

VAR userEntry = document.querySelector ( '#newIn')。値;

+0

私はそれを変更しました。しかし、削除ボタンをクリックするとdivを削除したい。 –

+0

私はそれを変更しましたが、作成したボタンを選択してdivを削除する方法を知りたいだけです。 –

+0

document.getElementById( 'Remove')。addEventListener( 'click'、function(){ console.log( 'done'); }); –

0

var Addnew = document.querySelector('#add_new'); 
 
var myDiv; 
 
var button; 
 

 
function newDiv() { 
 
    var body = document.getElementsByTagName('body')[0]; 
 
    myDiv = document.createElement('div'); 
 
    var userEntry = document.querySelector('.newIn').value; 
 
    button = document.createElement('button'); 
 
    myDiv.className = 'item'; 
 
    myDiv.style.display = 'block'; 
 
    myDiv.innerHTML = userEntry; 
 
    button.style.display = 'block'; 
 
    button.innerHTML = "Remove"; 
 
    button.setAttribute('id','Remove'); 
 
    button.style.marginLeft = '280px'; 
 
    button.style.background = 'tomato'; 
 
    button.style.color = 'white'; 
 
    myDiv.appendChild(button); 
 
    body.appendChild(myDiv); 
 
    button.addEventListener('click', function() { 
 
    console.log('done'); 
 
    }); 
 

 
} 
 

 

 
Addnew.addEventListener('click', function() { 
 
    newDiv(); 
 
}); 
 
document.querySelector('.newIn').addEventListener('keydown', function(e) { 
 
    if (e.which == 13) { 
 
     newDiv(); 
 
    } 
 
});
.input_box{ 
 
    text-align: center; 
 
    margin-top: 7%; 
 
} 
 
.input_box > input { 
 
    width:250px; 
 
} 
 
#add_new{ 
 
    background-color:rgb(44, 147, 124); 
 
    color:white; 
 
    width:70px; 
 
    margin-left:5px; 
 
} 
 
h1{ 
 
    text-align: center; 
 
    font-family:sans-serif;; 
 
} 
 
.item{ 
 
    width:270px; 
 
    height:35px; 
 
    margin-top: 20px; 
 
    margin-left:38%; 
 
    display: none; 
 
    border: 0.5px ridge green; 
 
} 
 
#Remove { 
 
    display: none; 
 
}
<!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>To-do List</title> 
 
    <!-- Css linking --> 
 
    <link rel="stylesheet" href="styles.css"> 
 
    </head> 
 
    <body> 
 
    <h1>To-do list</h1> 
 
    <div class="input_box"> 
 
     <input type="text" class="newIn" placeholder="What do you want to add?" autofocus="autofocus"/> 
 
     <button id="add_new">Add</button> 
 
    </div> 
 
    <div class="item"></div> <button id="Remove"></button> 
 
    <script src="main.js" charset="utf-8"></script> 
 
    </body> 
 
</html>

var Addnew = document.querySelector('#add_new'); 
 
var myDiv; 
 
var button; 
 

 
function newDiv() { 
 
    var body = document.getElementsByTagName('body')[0]; 
 
    myDiv = document.createElement('div'); 
 
    var userEntry = document.querySelector('.newIn').value; 
 
    button = document.createElement('button'); 
 
    myDiv.className = 'item'; 
 
    myDiv.style.display = 'block'; 
 
    myDiv.innerHTML = userEntry; 
 
    button.style.display = 'block'; 
 
    button.innerHTML = "Remove"; 
 
    button.setAttribute('id','Remove'); 
 
    button.style.marginLeft = '280px'; 
 
    button.style.background = 'tomato'; 
 
    button.style.color = 'white'; 
 
    myDiv.appendChild(button); 
 
    body.appendChild(myDiv); 
 
} 
 
document.getElementById('Remove').addEventListener('click', function() { 
 
    console.log('done'); 
 
}); 
 

 
Addnew.addEventListener('click', function() { 
 
    newDiv(); 
 
}); 
 
document.querySelector('.newIn').addEventListener('keydown', function(e) { 
 
    if (e.which == 13) { 
 
     newDiv(); 
 
    } 
 
});
.input_box{ 
 
    text-align: center; 
 
    margin-top: 7%; 
 
} 
 
.input_box > input { 
 
    width:250px; 
 
} 
 
#add_new{ 
 
    background-color:rgb(44, 147, 124); 
 
    color:white; 
 
    width:70px; 
 
    margin-left:5px; 
 
} 
 
h1{ 
 
    text-align: center; 
 
    font-family:sans-serif;; 
 
} 
 
.item{ 
 
    width:270px; 
 
    height:35px; 
 
    margin-top: 20px; 
 
    margin-left:38%; 
 
    display: none; 
 
    border: 0.5px ridge green; 
 
} 
 
#Remove { 
 
    display: none; 
 
}
<!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>To-do List</title> 
 
    <!-- Css linking --> 
 
    <link rel="stylesheet" href="styles.css"> 
 
    </head> 
 
    <body> 
 
    <h1>To-do list</h1> 
 
    <div class="input_box"> 
 
     <input type="text" class="newIn" placeholder="What do you want to add?" autofocus="autofocus"/> 
 
     <button id="add_new">Add</button> 
 
    </div> 
 
    <div class="item"></div> <button id="Remove"></button> 
 
    <script src="main.js" charset="utf-8"></script> 
 
    </body> 
 
</html>

関連する問題