Home > Web Front-end > JS Tutorial > body text

Discussion on the use of javascript:void(0)_javascript skills

WBOY
Release: 2016-05-16 17:24:24
Original
948 people have browsed it

When making a page, if you want to do nothing after clicking a link, or complete other things in response to the click, you can set its attribute href = "#". However, there will be a problem when the page has a scroll bar. , it will return to the top of the page after clicking, which is a bad user experience.

There are currently several solutions:
1) Do nothing after clicking the link

Copy code The code is as follows:

test
test
test //Use 2 to 4 #, most of them are "####", but also " #all" and others

2) After clicking the link, respond to the user-defined click event
Copy code The code is as follows:

Explanation:
1.javascript:void(0) This pseudo Protocols should be written less. If you have read some web standards books, you will know why. (I don’t understand, I’m excerpting the original words, so I’ll record them temporarily)
2. Directly using javascript:void(0) for links (href) may cause some problems in IE, such as causing the gif animation to stop playing, etc., so, The safest way is to use "####". To prevent jumping to the top of the page after clicking the link, the onclick event can return false.
3. If you just want to move the mouse over and turn it into a hand shape, you can use
to copy the code The code is as follows:

Click Me!

void is the operator of javascript, It means: only the expression is executed, but there is no return value. The
void operator usage format is as follows:
Copy code Code As follows:

javascript:void (expression)
javascript:void expression

For good program style, it is recommended to use the second type of
with brackets We can use the void operator to specify a hyperlink, such as javascript:void(document.form.submit()). The expression will be evaluated but nothing will be loaded into the current document. void(0) evaluates to 0 but has no effect on JavaScript, i.e. .
The key is to know that void is the operator of javascipt itself. It means that only the expression is executed, but there is no return value!

In addition, the page will automatically move back to the top because the default aiming point position of "#" is top, so this happens.
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template