Javascript simulated Click Event (Click link and HTML click) compatible with IE/Firefox

Source: Internet
Author: User

In one case, two aspects are simulated: click a super connection event.
Firefox compatible Functions
Add an onclick event to htmlanchorelement

CopyCode The Code is as follows: Try {
// Create a element so that htmlanchorelement is accessible
Document. createelement ('A ');
Htmlelement. Prototype. Click = function (){
If (typeof this. onclick = 'function '){
If (this. onclick ({type: 'click'}) & this. href)
Window. Open (this. href, this.tar get? This.tar get: '_ Self ');
}
Else if (this. href)
Window. Open (this. href, this.tar get? This.tar get: '_ Self ');
};
}
Catch (e ){
// Alert ('click Method for htmlanchorelement couldn \'t be added ');
}

The following are specific applications:
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

if it is an ordinary HTML add click
This section makes Firefox's htmlelement have the click method (add click method to htmlelement in Mozilla) copy Code the code is as follows: try {
// create span element so that htmlelement is accessible
document. createelement ('span ');
htmlelement. prototype. click = function () {
If (typeof this. onclick = 'function')
This. onclick ({type: 'click'});
};
}< br> catch (E) {
// alert ('click Method for htmlelement couldn \'t be added');
}

The following are other related articles .
recently, it was found that the user press enter in the webpage input box is not fixed...
especially when there are multiple forms on the webpage
I searched for a JS with a simulated click, copy Code the code is as follows: function doclick (linkid, e) {
If (E. keycode! = 13) {
return;
}< br> var fireonthis = document. getelementbyid (linkid)
If (document. createevent)
{< br> var evobj = document. createevent ('mouseevents')
evobj. initevent ('click', true, false)
fireonthis. dispatchevent (evobj)
}< br> else if (document. createeventobject)
{< br> fireonthis. fireevent ('onclick')
}< BR >}

E is an event, built-in object, and linkid is the ID of the simulated clicked object
for example,
in this way, you can press enter to submit the form ~
change opera to copy Code the code is as follows:

click me

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.