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中执行,将会有意想不到的收获。

  • 相关阅读:
    常用命令
    linux是文件里的内容整齐
    centos 7新机使用前操作
    Symmetric Tree @leetcode
    Binary Tree Inorder Traversal @leetcode
    [转]快速排序
    Remove Duplicates from Sorted Array @leetcode
    Remove Element @leetcode
    随笔1
    Unique Binary Search Trees @leetcode
  • 原文地址:https://www.cnblogs.com/dxzg/p/6409676.html
Copyright © 2011-2022 走看看