The usage of "href=" in label A

Source: Internet
Author: User

As we all know, the most important function of a label is to implement hyperlinks and anchor points. Also, most people think that the most important role of a tag is to implement hyperlinks, and today I happen to be writing a label <a href= "JavaScript:;" ></a>, so let's sort out some of the uses of href in the a tag.

One, JS several calling methods (refer to the summary)

1, a href= "Javascript:jS_method (); "

This is a common method, but this approach can be problematic when passing parameters such as this, and JavaScript: The protocol as the HREF attribute of a will not only lead to unnecessary triggering of window.onbeforeunload events, but will also stop the animated GIF pictures in IE. The standard of the consortium does not recommend JavaScript statements to be executed in href

2, a href= "javascript:void (0);" onclick= "Js_method () "

This method is a lot of web site most commonly used method, is also the most comprehensive method, the OnClick method is responsible for the implementation of JS function, and void is an operator, void (0) return undefined, address does not occur jump. Moreover, this method does not expose the JS method directly to the status bar of the browser as in the first method.

3, a href= "javascript:;" onclick= "Js_method () "

This method is similar to 2 kinds, the difference is only the execution of an empty JS code.

4, a href= "#" onclick= "Js_method () "

This method is also a very common online code, #是标签内置的一个方法, representing the role of top. So in this way, click on the Back page and return to the top of the page.

5, a href= "#" onclick= "Js_method (); return false;"

This method clicks on the implementation of the JS function return false, the page does not occur jump, after execution or in the current position of the page.

To synthesize the above, the most appropriate method of calling the JS function in a is recommended:
[JavaScript]View plain copy <a href= "javascript:void (0);" onclick= "Js_method ()" ></a> <a href= "javascript:;" onclick = "Js_method ()" ></a> <a href= "#" onclick= "Js_method (); return false;" ></a>second, the role of href= "#"

A href= "#" means back to the top. If scrolling is required on the current page, you can go straight back to the top in this way. For example, some sites in the lower right corner to make an icon button, back to the top, then you can consider this in the simplest way to achieve.

[JavaScript] view plain copy <span style= "FONT-SIZE:14PX;" ><a href= "#" > Back to Top </a></span> third, href= "URL" function

1, url is absolute URL

Point to another site, such as href= "Http://write.blog.csdn.NET", then the click will jump directly to the link to the page.

2, url is relative URL

Point to a file within the site, such as href= "/test.doc", then click to download the file directly.

Note : When you click on the <a> tab, the file will not be downloaded, but will open a new tab in the browser when you have a file type of. txt or pictures that can be opened directly on the page.

and displays the file/Picture contents in this page label. At this point, you can add the download attribute to the A element so that all files linked to a element are downloaded.

3, Anchor URL

Point to the anchor in the page, such as href= "#top", then click on the current page will be id= "top" of this anchor point, to achieve the current page of the so-called jump. The most important thing to do is to add a menu to a scrollable page and return it directly to a section of the page.

That's all three code examples:

[JavaScript] view plain copy <a href= "http://baidu.com" > Hyperlinks </a> <a href= "#" > Back to Top </a>   ; <a href= "css/css1.css" download> file links </a>

Note: Assign property values to the download property, such as Download= ' Demo.css ', to rename the download file.

However, if the file is in the current server, that is, it cannot be a cross-domain file.


Iv. The role of href= "mailto:"

When href= "mailto:", you can implement the function of sending mail.

The properties are as follows:

Subject is the subject of the message

The body is the content of the message;

CC to CC

Example:

<a href= "mailto:xuyong199312@sina.com?subject= Mail subject &cc=xuyong199312@sina.com&body= message content" > Send Message < /a>
Notes: In addition to using <a> tags to send mail, you can also use the <form action= "mailto:" title= "Mail" > or Java implementation to send mail function, please Baidu, online

A lot of relevant information.

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.