Discussion on the problem use of javascript:void (0)

Source: Internet
Author: User

  Want to do a link after clicking do nothing, there are many ways, but not to the user experience, Javascript:void (0) can effectively solve this problem

When making a page, if you want to do a link click Do not do anything, or respond to click and complete other things, you can set its properties href = "#", however, this will have a problem, that is, when the page has a scroll bar, click will return to the top of the page, the user experience is not good.     Currently there are several Solutions:  1) Click on the link after doing nothing   code as follows: <a href= "javascript:void (0);" >test</a>  <a href= "javascript:;" >test</a>  <a href= "#" >test</a>//Use 2 to 4 #, see Most is "#", also have use " #all "et     2) Click on the link to respond to user-defined click events     code as follows: <a href=" javascript:void (0) "onclick=" dosomething () ">test</a>  <a href=" # "onclick=" dosomething (); return false; " > issues are resolved, including browser incompatibility </a>//or direct use of href= ""   <a href= "# onclick=" alert (); event.returnvalue=false; " >test</a>    Description:  1.javascript:void (0) This pseudo agreement, write less good, if you have read some Web standard books to know why. (Do not understand, the words picked, temporary record)   2. Link (href) direct use of javascript:void (0) in IE may cause some problems, such as: GIF animation to stop playing, so, the safest way to use the "#". To prevent clicking on the link to jump to the top of the page, the OnClick event return false.   3. If you just want to move the mouse over and become a hand, you can use the   copy code as follows: <span style= "Cursor:pointer" onclick= "foo ()" >click Me!</span >&nbsP   Void is a JavaScript operator, meaning that only the expression is executed, but there is no return value,  void operator usage format is as follows:    code as follows: javascript:void (expression)   Javascript:void expression    for the program style is good, we recommend the use of the second bracket   we can use the void operator to specify a hyperlink, such as Javascript:void ( Document.form.submit ()). The expression is evaluated but does not load any content in the current document, void (0) evaluates to 0, but has no effect on JavaScript, meaning that <a href= "javascript:void (0)" > has the same effect as <a href= " Javascript:void (1) "> effect is the same.   The key is to know that void is the operator of Javascipt itself, it represents only the execution of expressions, but no return value!     Another page will automatically be recalled to the top because the "#" Default point of sight is tops, so this can happen.    
Related Article

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.