Javascript_Dom编程 阅读笔记(1)

寻找html页面中的节点

所有文档节点getElementsByTagName("*"); document.getElementsByTagName("*")

文档节点 getElementsByTagName; document.getElementsByTagName("div")

id getElementById; document.getElementById("id_")

类class

 function getElementsByClassName(node,classname){
if(node.getElementsByClassName){
  return node.getElementsByClassName(className);//使用现有方法
}else{
var results = new Array()
        ,elems = node.getElementsByTagName("*")
  ,i=0
        ,elemslength = elems.length;
for( i = 0;i<elemslength;i++){
  if(elems[i].className.indexof(classname)!= -1){
results.push(elems[i]);
}
}
return results;
}
}
    getElementsByClassName(document.getElementById("domid"),"classname_");    
    getElementsByClassName(document,"classname_");
             var $ = function(str){
var results;
if(typeof str === 'string'){
var firstchar = str.substring(0,1),
selectchars = str.substring(1,str.length);
if(firstchar==='#'){
results = document.getElementById(selectchars);
}
else if(firstchar==='.'){
results = [];
var elements = document.getElementsByTagName("*"),
i = 0,
elements_length = elements.length;
for(i = 0;i<elements_length;i++){
if(elements[i].className===selectchars){
results.push(elements[i]);
}
}
}
}
return results;
}
console.info($("#box"));
var clas = $(".cc"),
i = 0,
clasLength = clas.length;
for(i = 0;i<clasLength;i++){
console.info(clas[i]);
}

设置dom属性及其值

document.getElementById("domid").setAttribute("title","This is dom");

获取dom属性的值

document.getElementById("domid").getAttribute("title");
上一篇:JAVA学习基础知识总结(原创)


下一篇:Codeforces378 D Kostya the Sculptor(贪心)(逻辑)