Remove a link from a list of links
How can I remove a link from a list of links below if a link is clicked. The HTML for the links is a follows
<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 />
EDIT: Just realised that I need to <br />
to be removed too, so the remaining links move up. Looks weird with a gap where the link was removed.
EDIT: Here is the current script:
$(".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
}
$('a.standard_link').click(function(){ // when any link with the class "standard_link" is clicked
$(this).remove(); // remove it
});
NB that if you want to avoid the default event occuring, you'll need event.preventDefault()
:
$('a.standard_link').click(function(e){ // when any link with the class "standard_link" is clicked
e.preventDefault();
$(this).remove(); // remove it
});
Edit: to remove the <br/>
as well, use add
:
$(this).add(this.nextSibling).remove();
Note that this will break if there is anything (including white space) between the link and the line break.
$(".standard_link").click(function(){
$(this).remove();
});
Your question is a little vague. In truth there are many possibilities. It all depends on your exact requirement.
// 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();
If you want to target based on the text or html content of the link, you can do:
$("a.standard_link").filter(function() {
return $(this).text() == "link 2";
}).remove();
You don't really need JavaScript at all. In CSS, you can cause visited links to not display, for example:
a:visited { display: none; }
精彩评论