I recently read several posts about the <a> label and javascript: void (0). Keep this in mind for reference.
Note: The following code is not fully tested, but the possible situation of each method is basically described.
When you do a page, if you want to do nothing after clicking a link, or respond to the click to complete other tasks, you can set its property href = "#", however, in this case, when a page has a scroll bar, clicking it will return to the top of the page, and the user experience is poor.
There are currently several solutions:
1) do not do anything after clicking the link
Java code
- 1. <a href = "javascript: void (0);"> test </a>
- 2. <a href = "javascript:;"> test </a>
- 3. <a href = "####"> test </a> // use 2 to 4 #. For details, see "####", you can also use "# all" and other
2) click the link to respond to user-defined click events
Java code
- 1. <a href = "javascript: void (0)" onclick = "doSomething ()"> test </a>
- 2. <a href = "#" onclick = "doSomething (); return false;"> all problems have been solved, including browser incompatibility issues </a> // or use href = ""
- 3. <a href = "#" onclick = "alert (); event. returnValue = false;"> test </a>
Note:
1. javascript: the pseudo-Protocol void (0) is rarely written. If you have read some standard web books, you will know why. (Do not understand, the original words are extracted, and the record is made for now)
2. the direct use of javascript: void (0) by Link (href) in IE may cause some problems, such as: causes the GIF animation to stop playing. Therefore, the safest way is to use "####". To prevent the link from jump to the top, The onclick event return false.
3. You can use
Java code
- <Span style = "cursor: pointer" onclick = "foo ()"> Click Me! </Span>
Void is a javascript operator that only executes expressions but does not return values,
The format of the void operator is as follows:
Java code
- 1. javascript:Void(Expression)
- 2. javascript:VoidExpression
We recommend that you use the second type with parentheses for a good program style.
We can use the void operator to specify the hyperlink, such as javascript: void (document. form. submit ()). The expression is calculated, but no content is loaded in the current document. void (0) is calculated as 0, but it has no effect on JavaScript, that is, <a href = "javascript: the effect of void (0) "> is the same as that of <a href =" javascript: void (1) ">.
The key is as long as you know that void is the operator of javascipt itself, it indicates that only expressions are executed, but no return value is returned!
In addition, the page is automatically adjusted back to the top because "#" is the top position by default, so this situation occurs.