Some operations of JavaScript on Dom

Source: Internet
Author: User

After reading the video of Jiang Hao, I did his exercises and pasted the code to share them.

Javascript:

IC (function () {<br/> function $ () {<br/> var elements = new array (); <br/> // arguments is the currently passed parameter <br/> for (VAR I = 0; iIf (typeof element = 'string ') {<br/> element = document. getelementbyid (element); <br/>}< br/> If (arguments. length = 1) {<br/> return element; <br/>}< br/> elements. push (element); <br/>}< br/> return elements; <br/>}< br/> // register the namespace <br/> window ['ic '] ={}< br/> window ['ic'] ['$ '] = $; <br/> functi On getelementsbyclassname (classname, tag) {<br/> var alltags = document. getelementsbytagname (TAG); <br/> var matchingelements = new array (); </P> <p> classname = classname. replace (//-/g, "//-"); <br/> var RegEx = new Regexp ("(^ | // s) "+ classname +" (// s | $) "); </P> <p> var element; <br/> for (VAR I = 0; iIf (RegEx. test (element. classname) {<br/> matchingelements. push (element); <br/>}< br/> return matchingelemen TS; <br/>}</P> <p> window ['ic'] ['getelementsbyclassname'] = getelementsbyclassname; </P> <p> // IC. addevent () This method is used to add events to DOM nodes </P> <p> function addevent (Strid) {<br/> var anode = document. getelementbyid (Strid); <br/> anode. onclick = test; <br/> function test () {<br/> alert ("I want to register an event, succeeded? ") <Br/>}</P> <p> window ['ic'] ['addevent'] = addevent; </P> <p> // job, write common functions: </P> <p> // IC. insertafter () This method is used to add a node to the end of a DOM node <br/> function insertafter (Strid) {<br/> alert ("Check it out, add it "); <br/> var anode = document. getelementbyid (Strid); <br/> anode. innerhtml = ""; </P> <p >}</P> <p> window ['ic '] ['insertafter'] = insertafter; </P> <p> // IC. removechild () deletes multiple subnodes. <Br/> function removechild1 () {<br/> for (VAR I = 0; iIf (typeof element = 'string') {<br/> document. body. removechild (document. getelementbyid (element )); <br/>}</P> <p >}< br/>}</P> <p> window ['ic'] ['removechild1 '] = removechild1; </P> <p >}) (); <br/>

 

Then you can call it in an HTML file. I only give a small demo.
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <pead> <meta http-equiv =" content-Type "content =" text/html; charset = gb2312 "/> <title> untitled document </title> </pead> <MCE: Script Type =" text/JavaScript "src =" IC. JS "mce_src =" IC. JS "> </MCE: SCRIPT> <MCE: Script Type =" text/JavaScript "> <! -- Function testclick () {alert ("to delete .. "); IC. removechild1 ("testid2", "testd", "test") ;}// --> </MCE: SCRIPT> <body> <input type = "text" value = "test" class = "testme" id = "testd"/> <input type = "text" value = "ddddddddd "Class =" testme "id =" testid2 "/> <input type =" button "value =" testclick "id =" test "onclick =" testclick (); "/> <Div id =" adiv "> </div> </body> </ptml>

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.