admin管理员组

文章数量:1317558

I am creating a list of anchor tags from a MySQL/PHP query; the anchor tags call a JavaScript function.

The 'catch 22' that I have is:

  • href="#" makes the page jump to the top every time one of the anchor tags is clicked (very annoying)
  • removing href="#" means that the cursor does not change as an anchor tag is hovered over nor does that anchor tag have the appearance of an anchor tag.

I know there is a way to handle this with JavaScript (possibly jQuery) but I don't recall how at the moment. However, I really prefer a simpler HTML fix (if one exists) that does not require me to get into JavaScript.

Edit:
"does not require me to get into JavaScript" == "does not require extensive changes in JavaScript."

I am creating a list of anchor tags from a MySQL/PHP query; the anchor tags call a JavaScript function.

The 'catch 22' that I have is:

  • href="#" makes the page jump to the top every time one of the anchor tags is clicked (very annoying)
  • removing href="#" means that the cursor does not change as an anchor tag is hovered over nor does that anchor tag have the appearance of an anchor tag.

I know there is a way to handle this with JavaScript (possibly jQuery) but I don't recall how at the moment. However, I really prefer a simpler HTML fix (if one exists) that does not require me to get into JavaScript.

Edit:
"does not require me to get into JavaScript" == "does not require extensive changes in JavaScript."

Share Improve this question edited Dec 10, 2020 at 13:52 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 27, 2011 at 15:24 John RJohn R 3,03613 gold badges51 silver badges62 bronze badges 1
  • You can just set cursor: pointer; on <a> tags if you need to (and other hover styles). This is CSS though, not HTML. – TylerH Commented Dec 10, 2020 at 16:47
Add a ment  | 

5 Answers 5

Reset to default 4

In your javascript function you should return false, or with jquery you can use preventDefault()

Example:

$('a').click(function(event) {
    event.preventDefault();
    // do something
});

Or in your case:

<a href=“#” onclick="foo();return false;">

Or change the href to javascript:void(0):

<a href="javascript:void(0)" onclick="foo();">

Ideally, your link degrades without javascript, so the third option will usually be avoided.

A simpler HTML fix for you: Personally speaking, for plain test links I just use href=""

For links that point to a javascript function I tend to use href="javascript:;". Either way you'll stop the page jumping.

do return false after onclick or from within the foo() function.

You need to call event.preventDefault(); somewhere in your click event handler. In vanilla javascript, this can be done like so:

<script type="text/javascript">
  function foo(e) {
    e.preventDefault();
    // other code
  }
</script>
<a href="#" onclick="foo(e);"></a>

Additionally, you can also return false:

<a href="#" onclick="foo();return false;/>

This can be also done in jQuery pretty simply:

$('.mylink').click(function(e) {
  e.preventDefault();
  // other code
});

The downside of using return false; is that is also prevents the event from bubbling up the DOM. If you don't care about event bubbling it's okay to use false, but personally I feel that it's better to use event.preventDefault(); unless you specifically want to stop event bubbling as well.

<html>
    <head>

    </head>
    <body>
    <style>
    a {
        text-decoration: underline;
    }
    a:hover {
        text-decoration: underline;
    }
    </style>
    <script type="text/javascript">
      function foo() {
        alert('Hello World');
      }
    </script>
    <a onclick="foo();">Click Here</a>
    </body>
    </html>

Just adding the CSS and removing the href seems fine.

本文标签: javascriptAnchor tags list creationStack Overflow