Home > Web Front-end > JS Tutorial > js batch setting element style and encapsulation css method tutorial similar to jquery

js batch setting element style and encapsulation css method tutorial similar to jquery

巴扎黑
Release: 2017-07-21 17:43:03
Original
1428 people have browsed it

1. Batch setting style setGroupCss

function setGroupCss(curEle,options){//通过检测options的数据类型,如果不是一个对象,则不能进行批量操作if(Object.prototype.toString.call(options)!=="[object Object]"){return;
            }//遍历对象中的每一项,调取setCss方法一个个进行设置即可for(var key in options){if(options.hasOwnProperty(key)){
                    setCss(curEle,key,options[key])
                }        
            }
        }
Copy after login

2. Encapsulating css method

//此方法实现了获取、单独设置、批量设置元素的样式值function css(curEle){var argTwo = arguments[1];if(typeof argTwo === "string"){//传递第二个参数是一个字符串,这样的话可能就是获取样式;为什么是可能呢?因为还需要判断是否存在第三个参数,如果第三个参数存在的话,不是获取了,而是在单独的设置样式属性值var argThree = arguments[2];if(typeof argThree === "undefined"){//第三个参数不存在// return getCss(curEle,argTwo);return getCss.apply(this,arguments)
                }//第三个参数存在则为单独设置// setCss(curEle,argTwo,argThree)setCss.apply(this,arguments)return;
            }
            argTwo = argTwo || 0;//这行是为了 防止argTwo不存在为undefined的时候,下面的toString会报错if(argTwo.toString()==="[object Object]"){//批量设置样式属性值setGroupCss.apply(this,arguments)
            }
        }
Copy after login

The above is the detailed content of js batch setting element style and encapsulation css method tutorial similar to jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template