2016-04-03 184 views
0

我試圖訪問li元素中的div,然後將其刪除。但我需要通過另一個父母div訪問它,其中包含li。我在這方面的嘗試沒有奏效。這是我曾嘗試過的:parentDiv.childNodes[0].removeChild(document.getElementsById('removeThisChild')如何從Javascript中的另一個孩子中刪除孩子?

有誰知道我該如何做到這一點?

謝謝:)

更新
下面是HTML代碼:

<head> 
 
\t <script type="text/javascript"> 
 
\t \t window.onload = function() { 
 
\t \t \t var ul = document.getElementById('ul') 
 
\t \t \t ul.childNodes[0].removeChild(document.getElementById('div')) 
 
\t \t } 
 
\t </script> 
 
</head> 
 
<body> 
 
<ul id="ul"> 
 
\t <li id="li">this is the LI text <div id='div'>this is the div text</div></li> 
 
</ul> 
 

 
</body> 
 
</html>

+2

你能不能顯示你的html標籤,比如html頁面。有很多選擇。 – 7urkm3n

+0

你可以給'div',你想刪除一個'id'嗎? – John

+0

我不知道你的代碼是否可以工作 - 但爲了糾正一個錯字 - 當你使用getElementById選擇元素時,你不會在Element中包含「s」:... parentDiv.childNodes [0] .removeChild(document .getElementById('removeThisChild').... – gavgrif

回答