Recommended 4 native JavaScript commonly used function _javascript techniques

Source: Internet
Author: User

"One" Add a listener event

Copy Code code as follows:

     addhandler:function (NODE,TYPE,FN) {if ( Node.addeventlistener) {
                 Node.addeventlistener (Type,fn,false); //False, set to bubbling event
            }
            else{
                node.attachevent (' On ' +type,function () {
                     fn.apply (node,arguments); In the Attachevent method, this does not point to node, so you need to use the Apply () method to change the
                });
           }
    }

"Two" sets the style of an element

Copy Code code as follows:

Setcss:function (Node,val) {//val:{' top ': ' 2px ', ' font-size ': ' 12px '}
For (Var v. in Val) {
Node.style.cssText + = '; '  + V + ': ' +val[v]; With Csstext you can set multiple properties at the same time, there is a benefit to avoid cssfloat,stylefloat judgment
}
}

"Three" gets the CSS class name element

Copy Code code as follows:

Parent is an optional parameter,
Getbyclassname:function (classname,parent) {
var elem = [],
node = parent!= undefined&&parent.nodetype==1?parent.getelementsbytagname (' * '): document.getElementsByTagName (' * '),
p = new RegExp ("(^|\\s)" +classname+ "(\\s|$)");
for (Var n=0,i=node.length;n<i;n++) {
if (P.test (Node[n].classname)) {
Elem.push (Node[n]);
}
}
return elem;
}

"Four" delete CSS class name

Copy Code code as follows:

Removeclassname:function (node,classname) {
var par = new RegExp (className, ' G ');
Node.classname = Node.className.replace (par, ');
}

The above 4 is I tidy very practical also need to often use the original JS function, recommended to the small partners, I hope to help.

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.