JavaScript预加载和在插入元素封装

复制代码
//封装加载
function addLoadEvent(func) {
    var oldonload = window.onload;
    if(typeof window.onload !== 'function'){
        window.onload = func;
    } 
    else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

//使用
function loadEvents() {
  // home
  prepareSlideshow();
  // about
  prepareInternalnav();
  // photos
  preparePlaceholder();
  prepareGallery();
  // live
  stripeTables();
  highlightRows();
  displayAbbreviations();
  // contact
  focusLabels();
  prepareForms();
}

// Load events
addLoadEvent(highlightPage);
addLoadEvent(loadEvents);








//封装插入元素

function insertAfter(newElement, targetElement) {
    var parent = targetElement.parentNode;
    if (parent.lastChild === targetElement) {
        parent.appendChild(newElement);
    } 
    else {
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}


//使用
var intro = document.getElementById('intro');
 var slideshow = document.createElement('div');
    slideshow.setAttribute('id','slideshow');

    var frame = document.createElement('img');
    frame.setAttribute('src','images/frame.gif');
    frame.setAttribute('alt','');
    frame.setAttribute('id','frame');
    slideshow.appendChild(frame);

    var preview = document.createElement('img');
    preview.setAttribute('src','images/slideshow.gif');
    preview.setAttribute('alt','a glimpse of what awaits you');
    preview.setAttribute('id','preview');
    slideshow.appendChild(preview);


//在这里
insertAfter(slideshow,intro);
复制代码

 

posted @   漫漫长路</>  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示