How do hyperlinks correctly invoke JavaScript functions _javascript tips

Source: Internet
Author: User

Click the hyperlink to invoke the JavaScript function, which is commonly used by people:

Copy Code code as follows:
<a href= "javascript:function ();" >

But there is a drawback, that is, click on the link, the GIF animation on the page will still.

Look at the following code:

Copy Code code as follows:
<script type= "Text/javascript" >
<!--
function Foo ()
{
Do something
}
-->
</script>

<a href= "Javascript:foo ();" > Keep GIF Animated links </a>

Solution approach:

Copy Code code as follows:
<a onclick= "Javascript:foo ();" > Links </a>

At this time does not affect the animation display, but after the mouse moved up, the mouse and hyperlink style does not change, although you can use the style sheet to change the mouse and hyperlink style, but after all, some cumbersome, and this kind of thinking is not good.

The following code is further examined:

Copy Code code as follows:
<a onclick= "Javascript:foo ();" href= "#" > Links </a>

We can find that although clicking on the link does not affect the animation, but the page is always rolling to the top, this effect is not what we want.

Final Solution:

Copy Code code as follows:
<a onclick= "Javascript:foo (); return false;" href= "#" > does not affect GIF links </a>

If you don't consider the problem with GIF pictures, the above methods are available.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.