When making a page, if you want to do nothing after clicking a link, or complete other things in response to clicking, you can set its attribute href = "#", but there will be a problem, that is, when the page has scroll bars , after clicking, it will return to the top of the page, which has a poor user experience.
Currently there are several solutions:
1) Do nothing after clicking on the link
The code copy is as follows:
<a href="javascript:void(0);" >test</a>
<a href="javascript:;" >test</a>
<a href="####" >test</a> //Use 2 to 4#, most of them are "####", and some use "#all" and other
2) After clicking on the link, respond to the user-defined click event
The code copy is as follows:
<a href="javascript:void(0)" onclick="doSomething()">test</a>
<a href="#" onclick="doSomething();return false;">All problems have been solved, including browser incompatibility issues</a> //or use href="" directly
<a href="#" onclick="alert();event.returnValue=false;">test</a>
illustrate:
1.javascript:void(0) pseudo-protocol, which is less written. If you have read some web standard books, you will know why. (I don't understand, I've picked the original words, and I'll keep a record of it)
2. Using javascript:void(0) directly in IE may cause some problems, such as: causing gif animation to stop playing, etc., so the safest way is to use "####". To prevent the onclick event from jumping to the top of the page after clicking the link, the onclick event can be returned false.
3. If you just want to move the mouse and turn it into a hand shape, you can use it
The code copy is as follows:
<span style="cursor:pointer" onclick="foo()">Click Me!</span>
void is the operator of javascript, which means: only expressions are executed, but no return value.
The usage format of the void operator is as follows:
The code copy is as follows:
javascript:void (expression)
javascript:void expression
For good program style, it is recommended to use the second type with brackets
We can use the void operator to specify a hyperlink, such as javascript:void(document.form.submit()). The expression will be calculated but will not load anything in the current document. void(0) is calculated as 0, but it has no effect on JavaScript, that is, <a href="javascript:void(0)"> The effect is the same as <a href="javascript:void(1)">.
The key is to know that void is the operator of javascipt itself, which means that only expressions are executed but no return value!
In addition, the page will be automatically adjusted back to the top because the default aiming position of "#" is top, so this situation occurs.