Two useful JavaScript tool function code

Source: Internet
Author: User

1. You can write various assignment statements in your work.
For example, the most common obj. style. Display = "NONE ";
If there are more than one assignment statement, obj. style will be dizzy in a row.

The following extend functions in my base. js can be used to assign values to attributes or even function handles in JSON format.CopyCodeThe Code is as follows :/**
* Extended functions
* @ Param target refers to the object to be extended.
* @ Param Params attributes and methods to be put into the target
Function extend (target, Params ){
If (! Target ){
Target = {};
For (VAR prop in Params ){
Target [prop] = Params [prop];
Return target;

2. Because IE does not fully comply with W3C standards, its event model is different from that of other browsers.Different methods are called.
If you want to dynamically add events to the control. Using onclick = function () {} is not necessarily valid when Dom is dynamically created, and multiple handles cannot be bound. Next we will introduce a universal function that supports binding events to the entire browser.
In most cases, usecapture uses false, so it is simply written to death.Copy codeThe Code is as follows :/**
* Dynamically create event handles
* @ Param control refers to the object to be extended.
* @ Param eventname event name
* @ Param FN function handle
Function addeventlistener (control, eventname, FN ){

If (window. attachevent ){
Control. attachevent ('on' + eventname, FN );
} Else {
Control. addeventlistener (eventname, FN, false );

For the sample of the above two functions, refer to otherArticleCode in

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: 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.