Javascript Analog Click event (click Link and HTML click) compatible Ie/firefox_javascript tips

Source: Internet
Author: User

A case of analog click in general two aspects, analog Click on the Super Connection event
Firefox's compatible functions are
Add onclick event to Htmlanchorelement

Copy Code code as follows:

try {
Create a element so this 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.target this.target: ' _self ');
}
else if (this.href)
window.open (This.href, this.target this.target: ' _self ');
};
}
catch (e) {
Alert (' click Method for Htmlanchorelement couldn\ ' t is added ');
}

Here are the specific applications
<script type= "Text/javascript" > try {document.createelement (' a '); HTMLElement.prototype.click = function () {if (typeof This.onclick = = ' function ') {if (This.onclick ({type: ' click '}) & amp;& this.href) window.open (This.href, this.target this.target: ' _self '); else if (this.href) window.open (This.href, this.target? This.target: ' _self '); }; catch (e) {} var url=location.href; var urllink= "Http://www.jb51.net/do/plus/sh_down.php?url=" +URL; document.write ("<font color=red> Cloud Community has opened the anti-theft chain, download please download from here </font>"); var myLink = document.getElementById ("Jb51link"); Mylink.href = Urllink; Mylink.click (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

If it's normal HTML add click
This section makes Firefox's htmlelement have a click Method (add click Methods to HtmlElement in Mozilla)
Copy Code code as follows:

try {
Create span element So, 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 is added ');
}

The following are other relevant articles of netizens can also refer to the following.
Recently doing something to find the user in the Web page input box to press Enter the behavior is not fixed ...
Especially when you have multiple forms on a Web page.
So I searched for a simulation click of JS, tested in Firefox and IE on the run
Copy Code code 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 ')
}
}

where E is event, built-in object, LinkId is the object ID that simulates being clicked
such as <input id= "test" onkeypress= "DoClick" ("ButtonID", event) ">
This will allow the user to press ENTER to submit the form
Opera can be changed again
Copy Code code 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.