2011-01-21 31 views
1

列表中删除一个链接我怎样才能从下方如果一个链接被点击的链接列表中删除的链接。为链接的HTML是如下从链接

<a href="http://www.mysite.com/page1.html" class="standard_link">link 1</a><br /> 
<a href="http://www.mysite.com/page2.html" class="standard_link">link 2</a><br /> 
<a href="http://www.mysite.com/page3.html" class="standard_link">link 3</a><br /> 
<a href="http://www.mysite.com/page4.html" class="standard_link">link 4</a><br /> 
<a href="http://www.mysite.com/page5.html" class="standard_link">link 5</a><br /> 

编辑:刚刚意识到,我需要<br />进行过删除,因此剩下的链接向上移动。链接被删除的空白看起来很奇怪。

编辑:这是当前的脚本:

$(".standard_link").live('click', function(event) 
{ 
    event.preventDefault(); 

    my_function($(this)) 
}); 

function my_function(link) 
{ 
    // do some stuff first 

    // remove the clicked link plus the <br /> 
    link.add(link.nextSibling).remove(); 

    // do some other stuff 
} 
+1

你想要哪个链接删除?你怎么知道的? – lonesomeday 2011-01-21 15:38:56

+0

无论点击哪一个。 – oshirowanen 2011-01-21 15:40:42

回答

3
$('a.standard_link').click(function(){ // when any link with the class "standard_link" is clicked 
    $(this).remove(); // remove it 
}); 

NB,如果你想避免默认的事件发生的历史,你需要event.preventDefault()

$('a.standard_link').click(function(e){ // when any link with the class "standard_link" is clicked 
    e.preventDefault(); 
    $(this).remove(); // remove it 
}); 

编辑:删除<br/>以及使用add

$(this).add(this.nextSibling).remove(); 

注意,如果有链接和换行符之间的任何(包括空格),这将打破。

+0

完美。谢谢。 – oshirowanen 2011-01-21 15:44:50

+0

我如何删除
?如果链接被删除,我希望其他链接向上移动。 – oshirowanen 2011-01-21 15:51:00

1
$(".standard_link").click(function(){ 
    $(this).remove(); 
}); 
+0

我如何会删除
吗?如果链接被删除,我希望其他链接向上移动。 – oshirowanen 2011-01-21 15:52:29

0

你并不真正需要的JavaScript在所有。在CSS中,可能导致访问的链接不显示,例如:

a:visited { display: none; } 
1

您的问题有点含糊。其实有many possibilities。这一切都取决于你的确切要求。

// link href contains 'page5' 
$("a[href*=page5]").remove(); 

// links ending with page5.html 
$("a[href$=page5\\.html]").remove(); 

// remove all links with some class 
$(".standard_link").remove(); 

如果您想根据链接的文本或HTML的内容,你可以这样做:

$("a.standard_link").filter(function() { 
    return $(this).text() == "link 2"; 
}).remove();