博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript dom编程艺术笔记之图片库的改进
阅读量:4674 次
发布时间:2019-06-09

本文共 1263 字,大约阅读时间需要 4 分钟。

dom的操作要遵守的原则

1.平稳退化

2.分离javascript

3.向后兼容

4.性能考虑

 

改进后的显示图片方法

 

function showpic(whichpic){    if(!document.getElementById("placeholder")) return false;    var source=whichpic.getAttribute("href");    var placeholder=document.getElementById("placeholder");    if(placeholder.nodeName!="IMG") return false;    placeholder.setAttribute("src",source);    if(document.getElementById("description")){    var text=whichpic.getAttribute("title")?whichpic.getAttribute("title"):"";    var description=document.getElementById("description");    if(description.firstChild.nodeType==3){        discription.firstChild.nodeValue=text;     }    }    return true;}

 

新添加的分离html和javascript方法

function prepareGallery(){    if(!document.getElementById) return false;    if(!document.getElementByTagName) return false;    if(!document.getElementById("imagegallery")) return false;    var gallery=document.getElementById("imagegallery");    var link=gallery.getElementByTagName("a");    for(var i=0; i

 

添加事件的方法

function addLoadEvent(func){    var oldοnlοad=window.onload;    if(typeof window.onload!='function'){        window.onload=func;    }else{        window.onload-function(){            oldonload();            func();        }    } }

转载于:https://www.cnblogs.com/wr314/p/5328599.html

你可能感兴趣的文章