JS captures events other than a button event

Source: Internet
Author: User

After using JavaScript For so long, I feel that JS is not pleased with the pleasure it brings to the page. However, due to the diversity of browsers, it is as unpredictable as the Kaleidoscope. It is good on IE, there was a problem on Firefox, and it was good on sogou. Because of the different browser versions, there were more or less bugs. For testers who did not know the program, they thought it would be good to make small changes, I don't know whether this modification was made for debugging and was released for more than half of the time. Alas, let's just talk a little bit about it. A classic example can also bring convenience to your future work. The following example shows how to capture events other than a button event in javascript:

Event we all know this represents an event, but the difference between different browsers is big. for IE and Firefox: event indicates window in IE. event; in Firefox, it indicates: event (or if Firefox fails, it can also be represented by methods such:

Function generates event () {// compatible with both the Writing Method of IE and ff, and obtains the event object
Alert (arguments [0] | window. Event) // under IE and FF, "[object]" is displayed.
VaR EVT = arguments [0] | window. Event
VaR element = EVT. srcelement | evt.tar get // in IE and FF

Alert (element. ID )}

) If the problem persists, the best solution is to add the parameter event to the event so that it is completely OK.

The following is an example:

In addition to clicking the txtcontents or btnoks button, the system prompts that the clicked location is correct. Clicking somewhere else on the webpage prompts that the clicked location is incorrect. Please pay attention to the code below:

/* Reference jquery */

<SCRIPT type = "text/JavaScript">
/* Ready */
$ (Document). Ready (function (){
Document. Body. onclick = showinfo; // click events elsewhere in the document

});

Function showinfo (event ){

VaR _ event = event? Event: window. event; // This is used to call the event object in IE or Firefox.

// In Firefox, the event source is event.tar get.
VaR _targetpai_event.tar get? _Event.tar get: _ event. srcelement; // This is to get the event source in IE or Firefox.

VaR _ id = _ target. ID;
If (_ id = "")
{
_ Id = _ target. parentnode. ID;
}
// Alert (_ id );
If (_ ID! = "Txtcontents" & _ ID! = "Btnoks ")
{
Alert ("click the correct location ");
}
Else
{
Alert ("Click correct! ");
}

}

</SCRIPT>

Haha, I believe you have read it and then click other places to hide the pop-up Div layer!

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.