Javascript_Dom编程 阅读笔记(1)
寻找html页面中的节点
所有文档节点getElementsByTagName("*"); document.getElementsByTagName("*")
文档节点 getElementsByTagName; document.getElementsByTagName("div")
id getElementById; document.getElementById("id_")
类class
1 function getElementsByClassName(node,classname){ 2 if(node.getElementsByClassName){ 3 return node.getElementsByClassName(className);//使用现有方法 4 }else{ 5 var results = new Array()
,elems = node.getElementsByTagName("*") 7 ,i=0
,elemslength = elems.length; 8 for( i = 0;i<elemslength;i++){ 9 if(elems[i].className.indexof(classname)!= -1){ 10 results.push(elems[i]); 11 } 12 } 13 return results; 14 } 15 }
getElementsByClassName(document.getElementById("domid"),"classname_");
getElementsByClassName(document,"classname_");
1 var $ = function(str){ 2 var results; 3 if(typeof str === 'string'){ 4 var firstchar = str.substring(0,1), 5 selectchars = str.substring(1,str.length); 6 if(firstchar==='#'){ 7 results = document.getElementById(selectchars); 8 } 9 else if(firstchar==='.'){ 10 results = []; 11 var elements = document.getElementsByTagName("*"), 12 i = 0, 13 elements_length = elements.length; 14 for(i = 0;i<elements_length;i++){ 15 if(elements[i].className===selectchars){ 16 results.push(elements[i]); 17 } 18 } 19 } 20 } 21 return results; 22 } 23 console.info($("#box")); 24 var clas = $(".cc"), 25 i = 0, 26 clasLength = clas.length; 27 for(i = 0;i<clasLength;i++){ 28 console.info(clas[i]); 29 }
设置dom属性及其值
document.getElementById("domid").setAttribute("title","This is dom");
获取dom属性的值
document.getElementById("domid").getAttribute("title");