可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm working on some JQuery to hide/show some content when I click a link. I can create something like:
<a href="#" onclick="jquery_stuff" />
But if I click that link while I'm scrolled down on a page, it will jump back up to the top of the page.
If I do something like:
<a href="" onclick="jquery_stuff" />
The page will reload, which rids the page of all the changes that javascript has made.
Something like this:
<a onclick="jquery_stuff" />
Will give me the desired effect, but it no longer shows up as a link. Is there some way to specify an empty anchor so I can assign a javascript handler to the onclick
event, without changing anything on the page or moving the scrollbar?
回答1:
Put a "return false;" on the second option:
<a href="" onclick="jquery_stuff; return false;" />
回答2:
You need to return false;
after the jquery_stuff
:
<a href="no-javascript.html" onclick="jquery_stuff(); return false;" />
This will cancel the default action.
回答3:
You can put simply as like below:
<a href="javascript:;" onclick="jquery_stuff">
回答4:
jQuery has a function built in for this called preventDefault
which can be found here:
http://api.jquery.com/event.preventDefault/
Here's their example:
<script>
$("a").click(function(event) {
event.preventDefault();
});
</script>
You can also build the link like this:
<a href="javascript:void(0)" onclick="myJsFunc();">Link</a>
回答5:
Check eyelidlessness' comment (use a button, not an anchor). Was just about to post the same advice. An anchor that doesn't link to a resource and merely executes a script should be implemented as a button.
Stop putting your event handlers in HTML, and stop using anchor tags
that don't serve anchor semantic purposes. Use a button and add the
click handler in your Javascript. Example: HTML <button
id="jquery_stuff">Label</button>
and JavaScript
$('#jquery_stuff').click(jquery_stuff);
. You can use CSS to style the
button to look like a link, by removing padding, borders, margin and
background-color, then adding your link styles (eg. color and
text-decoration). – eyelidlessness Oct 19 '10 at 17:40
回答6:
<a href="javascript:;" onclick="jquery">link</a>
href
requires something in there if you want it to not pop up errors in validators for html. The javascript:;
is a good place holder.
If you really want to use the #
:
<a href="#me" name="me" onclick="jquery">link</a>
Be careful with the return false;
, it halts default behaviours of whatever you are doing.
Also if your js is like a submit you may run into problems in internet explorer.
回答7:
<a href="javascript:// some helpful comment " onclick="jquery_stuff" />
回答8:
I usually use a <span>
and style it to look like a link when I need to accomplish something like this.
回答9:
<a href="#" class="falseLinks">Link1</a>
<a href="#" class="falseLinks">Link2</a>
etc..
JQUERY:
$(function() {
$("a.falseLinks").click(function() {
// ...other stuff you want to do when links is clicked
// This is the same as putting onclick="return false;" in HTML
return false;
})
});
@eyelidlessness was a bit douchy in the way he said it but he was right. This is the cleanest way to do it. Why use jQuery if you're then gonna go back to vanilla Javascript for stuff jQuery does easier and cleaner?
回答10:
Usually I do:
<a style="cursor:pointer;" onclick="whatever();">Whatever</a>
回答11:
How about this:
<a href="#nogo" onclick="foo();">some text</a>