2016-09-24 5 views
1

リストアイテムをクリックすると、正しく削除されますか?Javascript - リストアイテムを削除する

私はアイテムを削除するには、コードの次の行を使用していたが、代わりに、これは全体のリスト自体が削除されます:私はこれを行う方法にオンラインで検索されていると同じ種類のコードを保持します

var list = document.getElementById("shoppinglist"); 

list.onclick = function() { list.parentNode.removeChild(list);} 

出現すると私はこれを解決する方法がわかりません。私は、生成されたリストアイテムが "ショッピングリスト"の子であると仮定しました。

私はJavascriptの新機能です。これはルーキーミスですが、本当に助けていただければ幸いです。ありがとうございました。

<!doctype html> 
 
<html dir="ltr" lang="en-gb"> 
 
<head> 
 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
<style> 
 
body { 
 
\t /* Sets the width then uses the margin auto feature to centre the page in the browser */ 
 
\t width:800px; 
 
\t margin: 0px auto; /*0px top/bottom auto left/right */ 
 
\t font-size:10px; /* By default fonts are usually 16px but may not be in some browsers */ 
 
} 
 
p, li { 
 
\t font-size:1.5em; /* Set all text to be 1.5 * overall font size = 15 pixels */ 
 
} 
 
section { 
 
\t /* each of the two sections will use just under half the width of the width of the body */ 
 
\t width:395px; 
 
\t display:block; 
 
} 
 
#choices { 
 
\t /* float the choices list to the left of the page */ 
 
\t float:left; 
 
\t background-color: #FFFF99; 
 
} 
 
#cart { 
 
\t /* float the shopping cart to the right of the page */ 
 
\t float:right; 
 
\t background-color: #7FFF00; 
 
} 
 
.cartlist { 
 
\t /* Simplify the display of the lists, removing default bullet points and indentation */ 
 
\t list-style-type:none; 
 
\t margin:0px; 
 
\t padding:0px; 
 
\t width:inherit; 
 
} 
 
.cartlist li { 
 
\t /* Set each list item to be 2* the height of the text */ 
 
\t height:2em; 
 
} 
 
.cartlist li:nth-child(odd) { 
 
\t /* Colour odd list items ie first, third, fifth etc, a different colour */ 
 
\t background-color:#eee; 
 
} 
 
#outputinfo { 
 
\t /* prevents the DIV from joining the floating behaviour so it displays below the lists */ 
 
\t clear:both; 
 
} 
 
</style> 
 
</head> 
 
<body> 
 
<section id="choices"> 
 
\t <p>Available Choices</p> 
 
\t <ul id="sourcelist" class="cartlist"> 
 
\t \t <li data-value="2.99">&pound;2.99 : Chocolate</li> 
 
\t \t <li data-value="3.49">&pound;3.49 : Cereal</li> 
 
\t \t <li data-value="0.98">&pound;0.98 : Milk</li> 
 
\t \t <li data-value="0.89">&pound;0.89 : Bread</li> 
 
\t \t <li data-value="3.79">&pound;3.79 : Coffee</li> 
 
\t \t <li data-value="2.53">&pound;2.53 : Strawberries</li> 
 
\t \t <li data-value="3.89">&pound;3.89 : Cheesecake</li> 
 
\t </ul> 
 
</section> 
 
<section id="cart"> 
 
\t <p>Shopping Cart</p> 
 
\t <ul id="shoppinglist" class="cartlist"></ul> 
 
</section> 
 
<div id="outputinfo"> 
 
\t <p><button id="calctotal">Calculate Total</button> : <span id="totalresult"></span></p> 
 
</div> 
 
</body> 
 

 
<script> 
 

 
function getTargetElement(e) { 
 
\t var targetelement=null; 
 
\t targetelement=(e.srcElement || e.target || e.toElement) 
 
\t return targetelement; 
 
} 
 

 
function calcTotal() { 
 
\t var shoppinglist=document.getElementById("shoppinglist"); 
 
\t var total=0; 
 
\t for(i=0;i<shoppinglist.children.length;i++) { 
 
\t \t total+=parseFloat(shoppinglist.children[i].getAttribute("data-value")); 
 
\t } 
 
\t var totalresult=document.getElementById("totalresult"); 
 
\t totalresult.innerHTML="&pound;"+total.toFixed(2); 
 
} 
 

 
function handleEvent(e) { 
 
\t var listclicked=getTargetElement(e); 
 
\t var newlistitem=document.createElement("li"); 
 
\t var datavalue=listclicked.getAttribute("data-value"); 
 
\t newlistitem.setAttribute("data-value",datavalue); 
 
\t newlisttext=document.createTextNode(listclicked.innerHTML) 
 
\t newlistitem.appendChild(newlisttext); 
 
\t var shoppinglist = document.getElementById("shoppinglist"); 
 
\t shoppinglist.appendChild(newlistitem); 
 
\t 
 
\t var list = document.getElementById("shoppinglist"); 
 
\t 
 
\t list.onclick = function() { list.parentNode.removeChild(list);} 
 

 

 
\t console.log(listclicked); 
 
} 
 

 
function removeItem(e){ 
 

 
\t var listclicked=getTargetElement(e); 
 
\t var node = document.getElementById('shoppinglist'); 
 
\t listclicked.parentNode.removeChild(listclicked); 
 
} 
 

 
document.onreadystatechange = function(){ 
 
\t if(document.readyState=="complete") { 
 
\t var sourcelist=document.getElementById("sourcelist"); 
 
\t 
 
\t for(i=0;i<sourcelist.children.length;i++) { 
 
\t \t \t if(document.addEventListener) { 
 
\t \t \t \t sourcelist.children[i].addEventListener("click", handleEvent, false); 
 

 
\t \t \t } else { 
 
\t \t \t \t sourcelist.children[i].attachEvent("onclick", handleEvent); 
 
\t \t \t } 
 
\t \t 
 
\t \t var totalbutton=document.getElementById("calctotal"); 
 
\t \t if(document.addEventListener) { 
 
\t \t \t totalbutton.addEventListener("click",calcTotal,false); 
 
\t \t } else { 
 
\t \t \t totalbutton.attachEvent("onclick",calcTotal); 
 
\t \t } 
 

 
\t \t } 
 

 
\t } 
 
} 
 
</script> 
 
    
 
</html>

答えて

3

クリックしたLI要素だけをリスト全体として削除する必要はありません。あなたがネストされた要素を持っているように思わないので

、イベントの代表団は、あなたがnesten要素を望んでいた場合、あなたが使用することができ、将来のために

var list = document.getElementById("shoppinglist"); 

list.addEventListener('click', function(evt) { 
    list.removeChild(evt.target); 
},false); 

FIDDLE

少し楽になりelement.closest()

var list = document.getElementById("shoppinglist"); 

list.addEventListener('click', function(evt) { 
    var p = evt.target.closest('li'); 
    list.removeChild(p); 
}, false); 

古いブラウザではclosest()のサポートが多少不足していますが、いくつかのポリフィルがあります必要に応じてできる。

はまた、無なし、あなたのコードが

var sourcelist = document.getElementById("sourcelist"); 

for(i = 0; i < sourcelist.children.length; i++) { 
    sourcelist.children[i].addEventListener("click", handleEvent, false); 
    ... 

// and then 

function handleEvent(e) { 

    var list = document.getElementById("shoppinglist"); 

    list.addEventListener('click', function(evt) { 
     list.removeChild(evt.target); 
    },false); 
    .... 

の同等のリストに別のリスト項目を追加しますので、すべての時間もしない大である、あなたはイベントハンドラ内でイベントハンドラをバインドしていることに注意してください新しいイベントハンドラをバインドして追加しますが、単一のイベントハンドラが必要です。複数のイベントハンドラが同じ要素を何度も繰り返し削除しようとしますが、初めて削除されます。このよう

+0

これは非常に便利でした。私は非常に感謝します。それは私に多くの見方を与え、理解してくれました。私はまた、他のコメントや他の誰もがそれらを見て、彼らが有用であるとして見て褒めたい。ありがとうございました。 – user3361789

0
list.onclick = function() { list.parentNode.removeChild(list);} 

listリスト全体(<ul id='list'>)です。あなたはリスト全体をクリックして聞いています。次に、リスト全体の親ノードをlist.parentNode(これは<section id="cart">)とし、そのリストを削除します。

あなたのコードは、あなたがしたことを正確に行っています。

0

は:

list.removeChild(list.childNodes[indexToRemove]) 

あなたは削除するためにどのようなノードを指定する必要があります。あなたは、コンソールを開くことにより、クロームでこれをテストして、次のように貼り付けることができます。

var list = document.getElementById("shoppinglist"); 

console.log('List:', list) 
console.log('List children:', list.childNodes) 

var indexToRemove = 0; 

list.removeChild(list.childNodes[indexToRemove]); 

console.log('List:', list) 
console.log('List children:', list.childNodes) 
0

あなたがこれを使用することができます:

var list = document.getElementById("shoppinglist"); 
list.onclick = function(e) { e.target.parentNode.removeChild(e.target);} 

あなたはhttp://fiddle.jshell.net/hidoos/Lpz917vo/

targetcurrentTargetについての詳細を読んで、この例をチェックアウトすることができます
関連する問題