後,我有這個摧毀特定Li和它的內容點擊一個鏈接
<ul>
<li id="any_list">Content to be destroyed</li>
</ul>
與此鏈接: <a href="#" onclick="return deleteList(document.getElementById('any_list'))">remove the list "#any_list"</a>
我怎麼能寫deleteList()函數,以列表#any_list及其內容?
後,我有這個摧毀特定Li和它的內容點擊一個鏈接
<ul>
<li id="any_list">Content to be destroyed</li>
</ul>
與此鏈接: <a href="#" onclick="return deleteList(document.getElementById('any_list'))">remove the list "#any_list"</a>
我怎麼能寫deleteList()函數,以列表#any_list及其內容?
您可以使用parentNode
屬性獲取元素的父級,並且可以使用removeChild
方法刪除子元素。因此:
function deleteList(element) {
element.parentNode.removeChild(element);
}
甜!感謝我所需要的 – user908715
應該是#any_list。 – bozdoz
所以它應該會更新。 – KryptoniteDove
-1表示完全不必要的'.each',並且未能顯示如何綁定處理程序。 – Alnitak
這是最好不要污染的標記。首先給鏈接一個id。
<a href="#" id="delete_any_list">remove the list "#any_list"</a>
然後簡單
$('#delete_any_list').click(function(e){
$('#any_list').remove();
e.preventDefault();
});
首先,添加一個id
到你的錨和刪除任何內嵌JavaScript:
<a href="#" id="delete_any_list">remove the list "#any_list"</a>
[它被認爲是不良的做法,這些天來不斷居然把任何JS直接寫入標記]。
然後,在你的jQuery document.ready
功能補充一點:
$('#delete_any_list').click(function(ev) {
$('#any_list').remove();
});
$(function(){
$("#delete_any_list").click(function(e){
e.preventDefault();
$("#any_list").remove();
});
});
<a href="#" id="delete_any_list">Delete any list</a>
你想刪除整個'ul',或只是在'li'點擊? – Ben