JS的DOM方法封裝庫

2021-08-18 08:44:30 字數 1178 閱讀 8078

由於使用原生js寫**時,經常需要使用到dom方法,需要獲取各種節點,而且還要處理各瀏覽器的相容性。於是封裝乙個自己的相容各瀏覽器的dom封裝庫就勢在必行了

var utils = (function()else

return arr;}}

function getchildren(parentel)elseelse

}return childrenarr;}}

function prev(curel)else

return prevel;}}

function prevall(curel)

return prevarr;

}function next(curel)else

return nextel;}}

function nextall(curel)

return nextarr;

}function sibling(curel)

function siblings(curel)

function firstchild(parentel)

function lastchild(parentel)

function index(curel)

}function prependchild(parentel,newel)else

}function insertbefore(newel,oldel)

function insertafter(newel,oldel)else

}function addclass(el,clname)else

}}else

}function removeclass(el,clname)

}}else

}function hasclass(el,clname)else

}}else

}function setcss(el,stylename,stylevalue)

function css(el,stylename,stylevalue)else

}else if(arguments.length === 3)

}return

})();

該dom封裝庫包含了開發中常用的方法並且處理了相容性。在一些獲取節點的方法裡,需要注意的是:獲取到的節點或節點陣列需要用return將其返回,否則無法獲取。

js方法的封裝

為什麼要進行方法的封裝?在平時的生產環境中,我們經常寫出這樣的 function fadd ia,ib function fmul ia,ib 其本質上是這樣的 var fadd function ia,ib var fmul function ia,ib 這樣的話,我們就建立了兩個全域性變數,ia...

js常用DOM方法

介紹幾個js dom的常用方法 獲取元素節點 getelementbyid getelementsbytagname getelementsbyclassname 先寫乙個簡單的網頁做測試 test.html doctype html html head meta charset utf 8 tit...

js常用DOM方法

介紹幾個js dom的常用方法 獲取元素節點 getelementbyid getelementsbytagname getelementsbyclassname 先寫乙個簡單的網頁做測試 test.html doctype html html head meta charset utf 8 tit...