单击链接后销毁特定的li及其内容

时间:2011-10-08 19:07:12

标签: javascript jquery

我有这个

<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及其内容?

5 个答案:

答案 0 :(得分:3)

您可以使用parentNode属性获取元素的父元素,并且可以使用removeChild方法删除子元素。因此:

function deleteList(element) {
    element.parentNode.removeChild(element);
}

答案 1 :(得分:0)

http://jsfiddle.net/rMPdn/1/

试试这个,上面的例子: -

$("#any_list").each(function() {
        $(this).remove(); 
});

答案 2 :(得分:0)

最好不要污染标记。首先给链接一个id。

<a href="#" id="delete_any_list">remove the list "#any_list"</a>

然后简单地

$('#delete_any_list').click(function(e){
  $('#any_list').remove(); 
     e.preventDefault(); 
});

答案 3 :(得分:0)

首先,在您的主播中添加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();
});

请参阅http://jsfiddle.net/alnitak/MbZpV/

答案 4 :(得分:0)

$(function(){
    $("#delete_any_list").click(function(e){
      e.preventDefault();
      $("#any_list").remove();
    });
});

<a href="#" id="delete_any_list">Delete any list</a>