原生js的各种方法
- 原生js操作dom元素
-
var link = document.createElement( "link" );
link.type = "image/x-icon";
link.rel = "icon";
link.href = "https://i.alipayobjects.com/common/favicon/favicon.ico";
document.getElementsByTagName('head')[0].appendChild(link); -
/* * 滚轮事件只有firefox比较特殊,使用DOMMouseScroll; 其他浏览器使用mousewheel; * */ // firefox document.body.addEventListener("DOMMouseScroll", function(event) { var direction= event.detail && (event.detail > 0 ? "mousedown" : "mouseup"); console.log(direction); }); // chrome and ie document.body.onmousewheel = function (event) { event = event || window.event; var direction = event.wheelDelta && (event.wheelDelta > 0 ? "mouseup" : "mousedown"); console.log(direction); };
- function hasClass( elements,cName ){
- return !!elements.className.match( new RegExp( "(\\s|^)" + cName + "(\\s|$)") );
- };
- function addClass( elements,cName ){
- if( !hasClass( elements,cName ) ){
- elements.className += " " + cName;
- };
- };
- function removeClass( elements,cName ){
- if( hasClass( elements,cName ) ){
- elements.className = elements.className.replace( new RegExp( "(\\s|^)" + cName + "(\\s|$)" ), " " );
- };
- };