⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.js

📁 实现自动刷新页面 本例实现页面自动刷新的效果
💻 JS
📖 第 1 页 / 共 3 页
字号:
      // Some browsers match names when looking for ids, so check names anyway.      if (nodes == null) nodes = document.getElementsByName(orig);      if (nodes != null && nodes.length > 1) {        for (i = 0; i < nodes.length; i++) {          node = nodes.item(i);          if (node.type == "radio") node.checked = (node.value == val);        }      }      else ele.checked = (val == true);    }    else if (ele.type == "checkbox") ele.checked = val;    else ele.value = val;    return;  }  if (dwr.util._isHTMLElement(ele, "textarea")) {    ele.value = val;    return;  }  // If the value to be set is a DOM object then we try importing the node  // rather than serializing it out  if (val.nodeType) {    if (val.nodeType == 9 /*Node.DOCUMENT_NODE*/) val = val.documentElement;    val = dwr.util._importNode(ele.ownerDocument, val, true);    ele.appendChild(val);    return;  }  // Fall back to innerHTML  ele.innerHTML = val;};/** * @private Find multiple items in a select list and select them. Used by setValue() * @param ele The select list item * @param val The array of values to select */dwr.util._selectListItems = function(ele, val) {  // We deal with select list elements by selecting the matching option  // Begin by searching through the values  var found  = false;  var i;  var j;  for (i = 0; i < ele.options.length; i++) {    ele.options[i].selected = false;    for (j = 0; j < val.length; j++) {      if (ele.options[i].value == val[j]) {        ele.options[i].selected = true;      }    }  }  // If that fails then try searching through the visible text  if (found) return;  for (i = 0; i < ele.options.length; i++) {    for (j = 0; j < val.length; j++) {      if (ele.options[i].text == val[j]) {        ele.options[i].selected = true;      }    }  }};/** * @private Find an item in a select list and select it. Used by setValue() * @param ele The select list item * @param val The value to select */dwr.util._selectListItem = function(ele, val) {  // We deal with select list elements by selecting the matching option  // Begin by searching through the values  var found = false;  var i;  for (i = 0; i < ele.options.length; i++) {    if (ele.options[i].value == val) {      ele.options[i].selected = true;      found = true;    }    else {      ele.options[i].selected = false;    }  }  // If that fails then try searching through the visible text  if (found) return;  for (i = 0; i < ele.options.length; i++) {    if (ele.options[i].text == val) {      ele.options[i].selected = true;    }    else {      ele.options[i].selected = false;    }  }};/** * Read the current value for a given HTML element. * @see http://getahead.org/dwr/browser/util/getvalue */dwr.util.getValue = function(ele, options) {  if (options == null) options = {};  var orig = ele;  ele = dwr.util.byId(ele);  // We can work with names and need to sometimes for radio buttons, and IE has  // an annoying bug where  var nodes = document.getElementsByName(orig);  if (ele == null && nodes.length >= 1) {    ele = nodes.item(0);  }  if (ele == null) {    dwr.util._debug("getValue() can't find an element with id/name: " + orig + ".");    return "";  }  if (dwr.util._isHTMLElement(ele, "select")) {    // Using "type" property instead of "multiple" as "type" is an official     // client-side property since JS 1.1    if (ele.type == "select-multiple") {      var reply = new Array();      for (i = 0; i < ele.options.length; i++) {        var item = ele.options[i];        if (item.selected) {          if (item.value != null && item.value != "") {            reply.push(item.value);          }          else {            reply.push(item.text);          }        }      }      return reply;    }    else {      var sel = ele.selectedIndex;      if (sel != -1) {        var item = ele.options[sel];        if (item.value != null && item.value != "") {          return item.value;        }        return item.text;      }      else {        return "";      }    }  }  if (dwr.util._isHTMLElement(ele, "input")) {    if (ele.type == "radio") {      var node;      for (i = 0; i < nodes.length; i++) {        node = nodes.item(i);        if (node.type == "radio") {          if (node.checked) {            if (nodes.length > 1) return node.value;            else return true;          }        }      }    }    switch (ele.type) {    case "checkbox":    case "check-box":    case "radio":      // if (ele.checked && ele.value != "") return ele.value;      // else      return ele.checked;    default:      return ele.value;    }  }  if (dwr.util._isHTMLElement(ele, "textarea")) {    return ele.value;  }  if (dwr.util._shouldEscapeHtml(options)) {    if (ele.textContent) return ele.textContent;    else if (ele.innerText) return ele.innerText;  }  return ele.innerHTML;};/** * getText() is like getValue() except that it reads the text (and not the value) from select elements * @see http://getahead.org/dwr/browser/util/gettext */dwr.util.getText = function(ele) {  ele = dwr.util._getElementById(ele, "getText()");  if (ele == null) return null;  if (!dwr.util._isHTMLElement(ele, "select")) {    dwr.util._debug("getText() can only be used with select elements. Attempt to use: " + dwr.util._detailedTypeOf(ele) + " from  id: " + orig + ".");    return "";  }  // This is a bit of a scam because it assumes single select  // but I'm not sure how we should treat multi-select.  var sel = ele.selectedIndex;  if (sel != -1) {    return ele.options[sel].text;  }  else {    return "";  }};/** * Given a map, or a recursive structure consisting of arrays and maps, call  * setValue() for all leaf entries and use intermediate levels to form nested * element ids. * @see http://getahead.org/dwr/browser/util/setvalues */dwr.util.setValues = function(data, options) {  var prefix = "";  if (options && options.prefix) prefix = options.prefix;  if (options && options.idPrefix) prefix = options.idPrefix;  dwr.util._setValuesRecursive(data, prefix);};/** * @private Recursive helper for setValues() */dwr.util._setValuesRecursive = function(data, idpath) {  // Array containing objects -> add "[n]" to prefix and make recursive call  // for each item object  if (dwr.util._isArray(data) && data.length > 0 && dwr.util._isObject(data[0])) {    for (var i = 0; i < data.length; i++) {      dwr.util._setValuesRecursive(data[i], idpath+"["+i+"]");    }  }  // Object (not array) -> handle nested object properties  else if (dwr.util._isObject(data) && !dwr.util._isArray(data)) {    for (var prop in data) {      var subidpath = idpath ? idpath+"."+prop : prop;      // Object (not array), or array containing objects -> call ourselves recursively      if (dwr.util._isObject(data[prop]) && !dwr.util._isArray(data[prop])           || dwr.util._isArray(data[prop]) && data[prop].length > 0 && dwr.util._isObject(data[prop][0])) {        dwr.util._setValuesRecursive(data[prop], subidpath);      }      // Functions -> skip      else if (typeof data[prop] == "function") {        // NOP      }      // Only simple values left (or array of simple values, or empty array)      // -> call setValue()      else {        // Are there any elements with that id or name        if (dwr.util.byId(subidpath) != null || document.getElementsByName(subidpath).length >= 1) {          dwr.util.setValue(subidpath, data[prop]);        }      }    }  }};/** * Given a map, or a recursive structure consisting of arrays and maps, call  * getValue() for all leaf entries and use intermediate levels to form nested * element ids. * Given a string or element that refers to a form, create an object from the  * elements of the form. * @see http://getahead.org/dwr/browser/util/getvalues */dwr.util.getValues = function(data, options) {  var ele;  if (typeof data == "string") ele = dwr.util.byId(data);  if (dwr.util._isHTMLElement(data)) ele = data;  if (ele != null) {    return dwr.util.getFormValues(ele);  }  else {    var prefix = "";    if (options != null && options.prefix) prefix = options.prefix;    if (options != null && options.idPrefix) prefix = options.idPrefix;    dwr.util._getValuesRecursive(data, prefix);    return data;  }};/** * Given a string or element that refers to a form, create an object from the  * elements of the form. * @see http://getahead.org/dwr/browser/util/getvalues */dwr.util.getFormValues = function(ele) {  ele = dwr.util.byId(ele);  if (ele != null) {    if (ele.elements == null) {      alert("getFormValues() requires an object or reference to a form element.");      return null;    }    var reply = {};    var value;    for (var i = 0; i < ele.elements.length; i++) {      if (ele[i].type in {button:0,submit:0,reset:0,image:0,file:0}) continue;      if (ele[i].id != null) value = ele[i].id;      else if (ele[i].value != null) value = ele[i].value;      else value = "element" + i;      reply[value] = dwr.util.getValue(ele[i]);    }    return reply;  }};/** * @private Recursive helper for getValues(). */dwr.util._getValuesRecursive = function(data, idpath) {  // Array containing objects -> add "[n]" to idpath and make recursive call  // for each item object  if (dwr.util._isArray(data) && data.length > 0 && dwr.util._isObject(data[0])) {    for (var i = 0; i < data.length; i++) {      dwr.util._getValuesRecursive(data[i], idpath+"["+i+"]");    }  }  // Object (not array) -> handle nested object properties  else if (dwr.util._isObject(data) && !dwr.util._isArray(data)) {    for (var prop in data) {      var subidpath = idpath ? idpath+"."+prop : prop;      // Object, or array containing objects -> call ourselves recursively      if (dwr.util._isObject(data[prop]) && !dwr.util._isArray(data[prop])          || dwr.util._isArray(data[prop]) && data[prop].length > 0 && dwr.util._isObject(data[prop][0])) {        dwr.util._getValuesRecursive(data[prop], subidpath);      }      // Functions -> skip      else if (typeof data[prop] == "function") {        // NOP      }      // Only simple values left (or array of simple values, or empty array)      // -> call getValue()      else {        // Are there any elements with that id or name        if (dwr.util.byId(subidpath) != null || document.getElementsByName(subidpath).length >= 1) {          data[prop] = dwr.util.getValue(subidpath);        }      }    }  }};/** * Add options to a list from an array or map. * @see http://getahead.org/dwr/browser/lists */dwr.util.addOptions = function(ele, data/*, options*/) {  ele = dwr.util._getElementById(ele, "addOptions()");  if (ele == null) return;  // TODO: Restructure so we handle arguments to get proper options handling  // if (options == null) options = {};  var useOptions = dwr.util._isHTMLElement(ele, "select");  var useLi = dwr.util._isHTMLElement(ele, ["ul", "ol"]);  if (!useOptions && !useLi) {    dwr.util._debug("addOptions() can only be used with select/ul/ol elements. Attempt to use: " + dwr.util._detailedTypeOf(ele));    return;  }  if (data == null) return;  var text, value, li;  if (dwr.util._isArray(data)) {    // Loop through the data that we do have    for (var i = 0; i < data.length; i++) {      if (useOptions) {        if (arguments[2] != null) {          if (arguments[3] != null) {            text = dwr.util._getValueFrom(data[i], arguments[3]);            value = dwr.util._getValueFrom(data[i], arguments[2]);          }          else text = value = dwr.util._getValueFrom(data[i], arguments[2]);        }        else text = value = dwr.util._getValueFrom(data[i], arguments[3]);        if (text != null || value) ele.options[ele.options.length] = new Option(text, value);      }      else {        li = document.createElement("li");        value = dwr.util._getValueFrom(data[i], arguments[2]);        if (value != null) {          li.innerHTML = value;          ele.appendChild(li);        }      }    }  }  else if (arguments[3] != null) {    if (!useOptions) {      alert("dwr.util.addOptions can only create select lists from objects.");      return;    }    for (var prop in data) {      value = dwr.util._getValueFrom(data[prop], arguments[2]);      text = dwr.util._getValueFrom(data[prop], arguments[3]);      if (text || value) ele.options[ele.options.length] = new Option(text, value);    }  }  else {    if (!useOptions) {      dwr.util._debug("dwr.util.addOptions can only create select lists from objects.");      return;    }    for (var prop in data) {      if (typeof data[prop] != "function") {        if (arguments[2]) ele.options[ele.options.length] = new Option(prop, data[prop]);        else ele.options[ele.options.length] = new Option(data[prop], prop);      }    }  }  // All error routes through this function result in a return, so highlight now  dwr.util.highlight(ele, null); // TODO: forward options instead of null };/** * @private Get the data from an array function for dwr.util.addOptions */dwr.util._getValueFrom = function(data, method) {  if (method == null) return data;  else if (typeof method == 'function') return method(data);  else return data[method];};/** * Remove all the options from a select list (specified by id) * @see http://getahead.org/dwr/browser/lists */dwr.util.removeAllOptions = function(ele) {  ele = dwr.util._getElementById(ele, "removeAllOptions()");  if (ele == null) return;  var useOptions = dwr.util._isHTMLElement(ele, "select");  var useLi = dwr.util._isHTMLElement(ele, ["ul", "ol"]);  if (!useOptions && !useLi) {    dwr.util._debug("removeAllOptions() can only be used with select, ol and ul elements. Attempt to use: " + dwr.util._detailedTypeOf(ele));    return;  }  if (useOptions) {    ele.options.length = 0;  }  else {    while (ele.childNodes.length > 0) {      ele.removeChild(ele.firstChild);    }  }};/**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -