JavaScript or jquery simulation click on hyperlinks and buttons

Source: Internet
Author: User

Sometimes we need to automatically click on the page hyperlink or button, you can use JS or jquery using the program to click, the method is very simple, the button or hyperlink code is as follows:

1 <ahref= "url"Target= "_blank"onclick= "Closewin ();"ID= "ALink">Click the hyperlink</a>2 <ButtonID= "BTN"onclick= "Test ()">Click the button</Button>

Above is a hyperlink and a button, and hyperlinks and buttons are bound to click events

Look at the hyperlink first, if we intend to click on the hyperlink at the same time not only to trigger the event and jump to the href specified link, you can use:

< Script >     document.getElementById ("ALink"). Click (); </ Script >

This will enable the above functions, but if I only want to trigger the event and do not want to jump, there are two methods, the first is the href write href= "javascript:;"

Another way to do this is to introduce jquery and then trigger with jquery click events

< Script >     $ ("#alink"). Click (); </ Script >

So we don't jump.

If it is a button, use the following code directly:

1 <Script>2     //Real App click button3 document.getElementById ("btn"). Click ();4     //No click button, just perform the event that the button is bound to5 document.getElementById ("btn"). onclick ();6 </Script>

Note has been noted the difference, although the principle is different, but here for the user is the same effect

Since JS can complete these functions, you can also submit the form, the process and analog click similar, but the method is different

form buttons If the button is the same as the analog click, binding an event, if the button is type= "Submit", then give the form an ID such as: id= "Form1" then the form submission can be done in the following way:

1 <Script>2     //JS Submission Form3 document.getElementById ("Form1"). Submit ();4     //jquery Submission Form5     $("#form1"). Submit ();6 </Script>

The code is very simple, is to use the JS in the Submit () method, the event can be encapsulated into the function of flexible use, the other submitted form there are some other methods, the above is a common method of comparison

JavaScript or jquery simulation click on hyperlinks and buttons

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.