Definition and detailed explanation of operator void(0) in Javascript

零下一度
Release: 2017-05-17 16:06:22
Original
1473 people have browsed it

void in Javascript is an operator that specifies to calculate an expression but does not return a value.

The most critical thing in javascript:void(0) is the void keyword. void is a very important keyword in JavaScript. This operator specifies to calculate an expression but does not return a value.

void operator usage format is as follows:

1. javascript:void (expression) 2. javascript:void expression
Copy after login

expression is a Javascript standard expression to be evaluated. The parentheses outside the expression are optional, but are a good practice to write.

You can specify a hyperlink using the void operator. The expression will be evaluated but nothing will be loaded into the current document.

Example - Clicking a hyperlink does not jump

1: 2: 3: 4:
Copy after login

After clicking the link, the page will scroll up to the top of the page, #The default anchor point is #TOP (actual testing found that the scroll bar will scroll to the top ) The above four methods only represent a dead link and will not jump or return to the top.

Example - Why does location.href not jump automatically?

删除 function delete(id){ if(confirm("确实要删除[为什么location.href不自动跳转?]吗?")) { location.href="/delete.jsp?id=" + id; } }
Copy after login

No matter how you check the above code, there is no problem, and location.href="/delete.jsp?id=" + id; works well in other places. Why does this code work?

The reason is that void(0) changed the code to:

< a href="javascript:delete('123')">删除function delete(id) { if(confirm("确实要删除[为什么location.href不自动跳转?]吗?")) { location.href="/delete.jsp?id=" + id; } }
Copy after login

We found that the page jumped immediately and the corresponding data could be deleted normally. Why?

Because void is an operator, it will calculate an expression, but will not return a value. Of course, it will not change any content of the current page, and it will not jump normally.

Explanation

voidOperatorEvaluates the expression and returns undefined. This operator is most useful when you want to evaluate an expression but don't want the result to be visible to the rest of the script.

Directly using javascript:void(0) for links (href) may cause some problems in IE, such as causing the gif animation to stop playing, etc. Therefore, 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.

【Related Recommendations】

1.Special Recommendation:"php Programmer Toolbox" V0.1 version Download

2.Free js online video tutorial

#3.

php.cn Dugu Jiujian (3) - JavaScript video tutorial##4.

Solution to javascript:void(0) under IE6 Invalid methods

5.

Summary of the difference between href=javascript:void(0) and href=

#6.

Use with caution javascript:void(0), why is it not good to write like this

The above is the detailed content of Definition and detailed explanation of operator void(0) in Javascript. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!