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
Copy codeThe 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 a common html add click
This section makes FireFox's HTMLElement have the click method (add click method to HTMLElement in Mozilla)
Copy codeThe 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 '});
};
}
Catch (e ){
// Alert ('click method for HTMLElement couldn \'t be added ');
}

The following are other articles related to netizens.
Recently, I found that it is not fixed for users to press enter in the webpage input box...
Especially when a webpage has multiple forms
So I searched for a js with a simulated click. It was tested to run on firefox and ie.
Copy codeThe Code is as follows:
Function doClick (linkId, e ){
If (e. keyCode! = 13 ){
Return;
}
Var fireOnThis = document. getElementById (linkId)
If (document. createEvent)
{
Var evObj = document. createEvent ('mouseevents ')
EvObj. initEvent ('click', true, false)
FireOnThis. dispatchEvent (evObj)
}
Else if (document. createEventObject)
{
FireOnThis. fireEvent ('onclick ')
}
}

E is an event, a built-in object, and a linkId is the id of the simulated clicked object.
For example, <INPUT id = "test" onkeypress = "doClick (" buttonId ", event)">
In this way, you can press enter to submit the form ~
Opera can be changed.
Copy codeThe Code is as follows:
<div onclick = "clickObj ('A ') "> click me </div>
<Script language = "javascript">
<! --
Function clickObj (o ){
Var o = document. getElementById (o );
If (document. all & typeof (document. all) = "object") // IE
{
O. fireEvent ("onclick ");
}
Else
{
Var e = document. createEvent ('mouseevent ');
E. initEvent ('click', false, false );
O. dispatchEvent (e );
}
}
// -->
</Script>

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.