"JavaScript" a tag onclick pass parameter is wrong, a tag call JS function summary

Source: Internet
Author: User

Error Example:
    1. <a href="Javascript:waterlineedit (${goods.goods_id})" > Modify </a>
    2. <!--The browser will actually be the following--
    3. <a href="Javascript:waterlineedit (20151333)" > Modify </a>
The original number is 20151333, click Edit should get 20151333 this only right. But it's not the number in the background. Very depressed. Then Baidu a tag call JS function Summary: 1, the first one of the wording  
    1. href="Javascript:js_method ();"
This is a common method, but this method is prone to problems when passing parameters such as this, and JavaScript: The protocol as the HREF attribute of a will not only cause unnecessary triggering of the Window.onbeforeunload event, but will also stop the animated GIF image from playing in IE. The standard is not recommended to execute JavaScript inside the href 2. The second way of writing
 
    1. href="javascript:void (0);" onclick="Js_method ()"
This method is the most common method of many websites, is also the most comprehensive method, the OnClick method is responsible for executing the JS function, and void is an operator, void (0) returns undefined, the address does not occur jump. And this method does not directly expose the JS method to the browser's status bar, as in the first method. 3. The third way of writing  
    1. Href="javascript:;" onclick="Js_method ()"

This method is similar to the 2, the difference is just the execution of an empty JS code.

4. The fourth kind of writing

 
    1. href="#" onclick="Js_method () "

This method is also very common on-line code, #是标签内置的一个方法, representing the role of top. So use this method to return to the top of the page after clicking on the page.

5. The fifth kind of writing

 
    1. href= "#" onclick="Js_method (); return false;"
This method click on the execution of the JS function return false, the page does not jump, after execution or in the current position of the page.

To synthesize the above, it is recommended to call the JS function in a in the most appropriate way:

    1. href="javascript:void (0);" onclick="Js_method ()"
    2. Href="javascript:;" onclick="Js_method ()"
    3. href= "#" onclick="Js_method (); return false;"
The character object in JS needs to be quoted:

So according to the changes, the results found still wrong. Finally found that the original is not added quotation marks caused by

 
  1. <a href="javascript:void (0)" onclick="Waterlineedit (' ${goods.goods.goods_id} ')"> Modify </a>
  2. <!--the above is correct, if the following is the last JS may think of an object passed in. Instead of string--
  3. <a href="javascript:void (0)" onclick="Waterlineedit (${goods.goods.goods_id})"> Modify </a>

"JavaScript" a tag onclick pass parameter is wrong, a tag call JS function summary

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.