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>