zoukankan      html  css  js  c++  java
  • 几个实用的js函数

    在阅读JavaScript DOM编程艺术这本书时看到了一些比较实用的代码。
    //加载多个window.onload事件
    function addLoadEvent(func) {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') {
      window.onload = func;
      } else {
      window.onload = function() {
      oldonload();
      func();
      }
     }
    }
    //在目标元素之后插入新元素
    function insertAfter(newElement,targetElement) {
      var parent = targetElement.parentNode;
      if (parent.lastChild == targetElement) {
      parent.appendChild(newElement);
      } else {
      parent.insertBefore(newElement,targetElement.nextSibling);
      }
    }
    //增加class样式
    function addClass(element,value) {
      if (!element.className) {
      element.className = value;
      } else {
      newClassName = element.className;
      newClassName+= " ";
      newClassName+= value;
      element.className = newClassName;
      }
    }

    [].forEach.call($$("*"),function(a){
    a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16)
    })

    将上面的代码放入console.log中执行,将会有意想不到的收获。

  • 相关阅读:
    P4630-[APIO2018]Duathlon铁人两项【圆方树】
    P4980-[模板]Pólya定理
    bzoj4589-Hard Nim【FWT】
    CF700E-Cool Slogans【SAM,线段树合并,dp】
    sqrt数据结构
    NOIP历年好题
    ACM题解吖
    [ZJOI2010]排列计数
    [CQOI2014]数三角形
    [SHOI2007]书柜的尺寸
  • 原文地址:https://www.cnblogs.com/dxzg/p/6409676.html
Copyright © 2011-2022 走看看