js函数备份

随机数:

function randomString(length){
  length = length || 32;
  var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789';
  var maxPos = chars.length;
  var string = '';
  for (i = 0; i < length; i++) {
      string += chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return string;
}

 

添加样式:

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
 }

addGlobalStyle("html, * {-moz-user-select:text!important;}");

 

用getElementsByTagName函数来测试页面中是否有 HTML 元素。

例:检查页面中是否有 <textarea> 元属
var textareas = document.getElementsByTagName('textarea');
if (textareas.length) { // 页面中有至少一个 textareaelse { // 页面中没有 textarea }

 

posted @ 2015-12-12 19:00  起一个  阅读(297)  评论(0)    收藏  举报