Dom编程艺术1-平稳退化

1.兼容getElementsByClassName

function getElementByClassName(node,classname){
    if(node.getElementsByClassName){
        //使用现有的方法
        return node.getElementsByClassName(classname);
    } else {
        var results=new Array();
        var elems = node.getElementsByTagName("*");
        for(var i=0;i

2.获取属性getAttribute,getAttribute

var shopping=document.getElementById("purchases");
alert(shopping.getAttribute("title"));
shopping.setAttribute("title","a list of goods");
alert(shopping.getAttribute("title"));

3.value 和 src

//下面两个等价
element.value="new value";
element.setAttribute("value","new value");

element.src="href";
element.setAttribute("src","new value");

4.childNodes

用下面语法获取节点的nodeType属性

node.nodeType
//nodeType属性共有12种可取值,但其中仅有3种有价值
1.元素节点的nodeType 属性值为1
2.属性节点的nodeType 属性值为2
3.文本节点的nodeType 属性值为3
```
如果想获得一个文本节点的值
```
node.nodeValue;
```
获取第一个和最后一个节点
```
node.firstChild == node.childNodes[0]
node.lastChild == node.childNodes[node.childNodes.length-1]
```
具体应用,动态切换图片
```
function showPic(whichpic) {
  var source = whichpic.getAttribute("href");
  var placeholder = document.getElementById("placeholder");
  placeholder.setAttribute("src",source);
  var text = whichpic.getAttribute("title");
  var description = document.getElementById("description");
  description.firstChild.nodeValue = text;
}

Fireworks

 my image gallery
```

####5.一个平稳退化的例子
```



  
  Example



//样式分离,并没有增加什么js代码
//Example
Example



```

####6.图片库代码改进
```
//点击事件处理函数
function showPic(whichpic) {
  if (!document.getElementById("placeholder")) return true;
  var source = whichpic.getAttribute("href");
  var placeholder = document.getElementById("placeholder");
  placeholder.setAttribute("src",source);
  if (!document.getElementById("description")) return false;
  //判断title属性是否存在
  if (whichpic.getAttribute("title")) {
    var text = whichpic.getAttribute("title");
  } else {
    var text = "";
  }
  var description = document.getElementById("description");
  if (description.firstChild.nodeType == 3) {
    description.firstChild.nodeValue = text;
  }
  return false;//不让浏览器执行默认行为
}

//为连接增加点击事件,一个函数只有一个出口并集中在开头位置
function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  //预防性措施
  if (!document.getElementById("imagegallery")) return false;
  var gallery = document.getElementById("imagegallery");
  var links = gallery.getElementsByTagName("a");
  for ( var i=0; i < links.length; i++) {
    links[i].onclick = function() {
      return showPic(this);
      }
    //最好不使用,因为onclick也支持按键
   // links[i].onkeypress = links[i].onclick;
  }
}
//共享onload事件
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(prepareGallery);
```
```



  
  Image Gallery
    
    


  

Snapshots

my image gallery

Choose an image.

``` ####7.动态创建标记 传统方法,没有做到样式和行为分离,不推荐 ``` ``` 把一大段html放入文档中innerHTML ``` var testdiv = document.getElementById("testdiv"); testdiv.innerHTML="

I inserted this content.

"; ``` #####dom方法 用到的这几个方法或者属性 parentNode 属性 lastChild 属性 appendChild 方法 insertBefore 方法 nextSibling 属性 ``` window.onload = function() { var para = document.createElement("p"); var txt1 = document.createTextNode("I inserted "); var emphasis = document.createElement("em"); var txt2 = document.createTextNode("this"); var txt3 = document.createTextNode(" content."); para.appendChild(txt1); emphasis.appendChild(txt2); para.appendChild(emphasis); para.appendChild(txt3); var testdiv = document.getElementById("testdiv"); testdiv.appendChild(para); } ``` 图片库代码,刚开始展示的文字和图片用js生成 #####注意插入之前gallery.parentNode.insertBefore(placeholder,gallery); #####没有insertAfter ``` function preparePlaceholder() { if (!document.createElement) return false; if (!document.createTextNode) return false; var placeholder = document.createElement("img"); placeholder.setAttribute("id","placeholder"); placeholder.setAttribute("src","images/placeholder.gif"); placeholder.setAttribute("alt","my image gallery"); var description = document.createElement("p"); description.setAttribute("id","description"); var desctext = document.createTextNode("Choose an image"); description.appendChild(desctext); var gallery = document.getElementById("imagegallery"); gallery.parentNode.insertBefore(placeholder,gallery); gallery.parentNode.insertBefore(description,gallery); } ``` 自己实现一个insertAfter ``` function insertAfter(newElement,targetElement) { var parent = targetElement.parentNode; if (parent.lastChild == targetElement) { parent.appendChild(newElement); } else { parent.insertBefore(newElement,targetElement.nextSibling); } } ```

你可能感兴趣的:(Dom编程艺术1-平稳退化)