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