html-utils

utils for DOM manipulation and fetching info of a webpage

当前为 2016-05-31 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/20131/128691/html-utils.js

  1. /**
  2. * Factory that creates customized html-elements
  3. */
  4. var HtmlFactory = function() {
  5. /**
  6. * Adds a HTML child node to a HTML parent node.
  7. * @param {HtmlElement} elParent - The html parent node
  8. * @param {HtmlElement} elChild - The html child Element to be connected to the parent node
  9. * @param {boolean} [asFirstChild] - <code>true</code> to have the child be added as the first child before any other child
  10. */
  11. var connectParentChild = function (elParent, elChild, asFirstChild) {
  12. if (asFirstChild && elParent.hasChildNodes()) {
  13. elParent.insertBefore(elChild, elParent.firstChild);
  14. } else {
  15. elParent.appendChild(elChild);
  16. }
  17. };
  18. /**
  19. * Removes a HTML child element from a HTML parent node.
  20. * @param {HTMLElement} elParent - The parent node
  21. * @param {HTMLElement} elChild - The child node to be removed of the parent element
  22. */
  23. var disconnectParentChild = function (elParent, elChild) {
  24. elParent.removeChild(elChild);
  25. };
  26. /**
  27. * Removes all HTML children from a parent node.
  28. * @param {HTMLElement} elParent - The HTML parent node
  29. */
  30. var removeAllChildren = function (elParent) {
  31. while (elParent.hasChildNodes()) {
  32. elParent.removeChild(elParent.firstChild);
  33. }
  34. };
  35.  
  36. /**
  37. * @name ElType
  38. * @type HtmlElement
  39. * @property {function} addAsLastChild
  40. * @property {function} addAsFirstChild
  41. * @property {function} addAsOnlyChild
  42. */
  43.  
  44. /**
  45. * Creates any type of HTML element.
  46. * @param {string} elType - The type of element to be created
  47. * @param {string} [id] - The value for the <code>id</code> attribute
  48. * @param {string} [className] - The value for the <code>class</code> attribute
  49. * @returns {ElType} The HTML element
  50. */
  51. var createEl = function(elType, id, className) {
  52. var _el = document.createElement(elType);
  53. // add params to html Element properties
  54. var args = { elType:elType, id:id, className:className };
  55. Object.keys(args).forEach(function(prop){
  56. if (args[prop] !== undefined) { _el[prop] = args[prop]; }
  57. });
  58. /**
  59. * Adds this HTML element to a parent node HTML element.
  60. * It will be added as the last child of the node.
  61. * @this createEl
  62. * @param {HTMLElement} elParent - The parent HTML node element
  63. */
  64. _el.addAsLastChild = function(elParent) { connectParentChild(elParent,_el, false); };
  65. /**
  66. * Adds this HTML element to a parent node HTML element.
  67. * It will be added as the first child of the node.
  68. * @this createEl
  69. * @param {HTMLElement} elParent - The parent HTML node element
  70. */
  71. _el.addAsFirstChild = function(elParent) { connectParentChild(elParent,_el, true); };
  72. /**
  73. * Adds this HTML element to a parent node HTML element.
  74. * All children elements will be removed and replaced with this node.
  75. * @this createEl
  76. * @param {HTMLElement} elParent - The parent HTML node element
  77. */
  78. _el.addAsOnlyChild = function(elParent) {
  79. removeAllChildren(elParent);
  80. connectParentChild(elParent, _el);
  81. };
  82. /**
  83. * Appends Html node elements to this node as their last children.
  84. * @param {...HTMLElement} elements - Html elements to be added as children.
  85. */
  86. _el.appendChildren = function(elements) {
  87. for (var i = 0; i <= arguments.length - 1; i++) {
  88. _el.appendChild(arguments[i]);
  89. }
  90. };
  91. /**
  92. * Adds Html node Elements to this node as their new children.
  93. * All current children elements will be removed and replaced with the new children.
  94. * @param {...HTMLElement} [elements] Html elements to be added as children
  95. * No argument will only remove all children.
  96. */
  97. _el.setChildren = function(elements) {
  98. removeAllChildren(_el);
  99. for (var i = 0; i <= arguments.length - 1; i++) {
  100. connectParentChild(_el, arguments[i]);
  101. }
  102. };
  103. return _el;
  104. };
  105. /**
  106. * Creates a 'Button' HTML element.
  107. * @param {string} [className] - The value for the <code>class</code> attribute
  108. * @param {function} [callback] - The callback function for the <code>onClick<code> event
  109. * @param {string} [text] - The content text of the element (text shown on the button)
  110. * @param {string} [id] - The value for the <code>id</code> attribute
  111. * @returns {ElType|HTMLElement} The 'button' HTML element
  112. */
  113. var createButton = function(className, callback, text, id) {
  114. var elButton = createEl('BUTTON', id, className);
  115. if (typeof callback === 'function') {
  116. elButton.addEventListener('click', callback);
  117. }
  118. if (text !== undefined) {
  119. elButton.appendChild(document.createTextNode(text));
  120. }
  121. return elButton;
  122. };
  123. /**
  124. * Creates a 'div' HTML element.
  125. * @param {string} [className] - The value for the <code>class</code> attribute
  126. * @param {string} [id] - The value for the <code>id</code> attribute
  127. * @returns {ElType|HTMLElement} The 'div' HTML element
  128. */
  129. var createDiv = function(className, id) {
  130. return createEl('div', id, className);
  131. };
  132. /**
  133. * Creates an 'input' HTML element as a field for text.
  134. * @param {number|string} [maxlength] - The maximum number of characters
  135. * @param {string} [text] - The value of the <code>text</code> attribute (content initially shown in the field)
  136. * @param {string} [className] - The value for the <code>class</code> attribute
  137. * @param {string} [id] - The value for the <code>id</code> attribute
  138. * @returns {ElType|HTMLElement} The 'input' HTML element
  139. */
  140. var createInput = function(maxlength, text, className, id) {
  141. var elInputText = createEl('INPUT', id, className);
  142. if (maxlength !== undefined) { elInputText.maxlength=maxlength; }
  143. if (text !== undefined) { elInputText.value = text; }
  144. return elInputText;
  145. };
  146. /**
  147. * Creates a 'label' HTML element.
  148. * @param {string} htmlFor - The value of the <code>for</code> attribute. The id value of another element to bind the label to that element.
  149. * @param {string} [text] - The content text of the element (text shown on the label)
  150. * @param {string} [className] - The value for the <code>class</code> attribute
  151. * @param {string} [id] - The value for the <code>id</code> attribute
  152. * @returns {ElType|HTMLElement} The 'label' HTML element
  153. */
  154. var createLabel = function (htmlFor, text, className, id) {
  155. var elLabel = createEl('LABEL', id, className);
  156. if (text !== undefined) {
  157. elLabel.appendChild(document.createTextNode(text));
  158. }
  159. if (htmlFor !== undefined) { elLabel.htmlFor = htmlFor; }
  160. return elLabel;
  161. };
  162. /**
  163. * Creates a 'TextArea' Html element.
  164. * @param {number|string} [cols] - The number of columns
  165. * @param {number|string} [maxlength] The maximum number of characters
  166. * @param {string} [text] - The value of the <code>text</code> attribute (content initially shown in the field)
  167. * @param {string} [className] - The value for the <code>class</code> attribute
  168. * @param {string} [id] - The value of the <code>id</code> attribute
  169. * @returns {ElType|HTMLElement} The 'textarea' HTML element
  170. */
  171. var createTextArea = function(cols, maxlength, text, className, id) {
  172. var elTextArea = createEl('TEXTAREA', id, className);
  173. if (cols != undefined) { elTextArea.cols = cols; }
  174. if (maxlength !== undefined) { elTextArea.maxlength = maxlength; }
  175. if (text !== undefined) { elTextArea.value = text; }
  176. return elTextArea;
  177. };
  178. /**
  179. * Sorts options of a html 'select' element by a certain property of the option (e.g. text or value).
  180. * @param {HTMLElement} elSelect - The Select Element to be sorted
  181. * @param {string} [propertyName=text] - The Name of the property by wich the option should be compared like <code>value</code> or <code>text</code>.
  182. * @param {boolean} [isOrderDescending] - <code>true</code> to reverse the sort order.
  183. */
  184. var sortSelectBy = function (elSelect, propertyName, isOrderDescending) {
  185. propertyName = propertyName || 'text';
  186. var sortOptions = [];
  187. // options to array
  188. for (var i = 0; i <= elSelect.length - 1; i++) {
  189. var option = elSelect[i];
  190. sortOptions.push({
  191. value:option.value,
  192. text:option.text,
  193. selected:option.selected
  194. });
  195. }
  196. // sort array
  197. sortOptions.sort(function (a, b) {
  198. if (a[propertyName] < b[propertyName]) { return isOrderDescending ? 1 : -1; }
  199. if (a[propertyName] > b[propertyName]) { return isOrderDescending ? -1 : 1; }
  200. return 0;
  201. });
  202. // array to options
  203. sortOptions.forEach(function (opt, i) {
  204. elSelect[i].text = opt.text;
  205. elSelect[i].value = opt.value;
  206. elSelect[i].selected = opt.selected;
  207. });
  208. };
  209. /**
  210. * Creates an 'Option' HTML element which can be added to a HTML select element.
  211. * @param text The value of the <code>text</code> attribute shown in the select dropdown
  212. * @param value THe value of the <code>value</code> attribute
  213. * @returns {HTMLOptionElement}
  214. */
  215. var createOption = function (text, value) {
  216. var elOption = document.createElement('OPTION');
  217. elOption.text = text;
  218. elOption.value = value;
  219. return elOption;
  220. };
  221. /**
  222. * Creates a 'Select' Html element without options.
  223. * @param {string} [className] - The value for the <code>class</code> attribute
  224. * @param {string} [id] - The value of the <code>id</code> attribute
  225. * @returns {ElType|HTMLElement} The 'Select' HTML element
  226. */
  227. var createSelect = function (className, id) {
  228. var elSelect = new createEl('SELECT', id, className);
  229. elSelect.addNewOption = function(text, value) {
  230. var elOption = document.createElement('OPTION');
  231. elOption.text = text;
  232. elOption.value = value;
  233. elSelect.add(elOption);
  234. return elOption;
  235. };
  236. /**
  237. * Sorts options of this select alphabetically by the values of their <code>text</code> attribute.
  238. * @param {boolean} [isOrderDesc] - <code>true</code> to sort options by descending order
  239. */
  240. elSelect.sortOptionsByText = function (isOrderDesc) {
  241. sortSelectBy(elSelect, 'text', isOrderDesc);
  242. };
  243. /**
  244. * Sorts options of this select alphabetically by the values of their <code>value</code> attribute.
  245. * @param {boolean} [isOrderDesc] - <code>true</code> to sort options by descending order
  246. */
  247. elSelect.sortOptionsByValue = function (isOrderDesc) {
  248. sortSelectBy(elSelect, 'value', isOrderDesc);
  249. };
  250. return elSelect;
  251. };
  252. return {
  253. newEl : createEl,
  254. newDiv : createDiv,
  255. newButton : createButton,
  256. newInput : createInput,
  257. newLabel : createLabel,
  258. newTextArea : createTextArea,
  259. newOption : createOption,
  260. newSelect : createSelect
  261. }
  262. }();
  263.  
  264.  
  265. var Page = function() {
  266. /**
  267. * Returns the value of a query parameter of the URI.
  268. * @param {string} paramName - The key (name) to identify the query parameter
  269. * @returns {string|null} The value of the paremeter or <code>null</code> if the uri doesn't define that parameter.
  270. */
  271. var getUriQueryParamValue = function(paramName) {
  272. return decodeURIComponent((new RegExp('[?|&]' + paramName + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
  273. };
  274.  
  275. /**
  276. * Sets or updates a query parameter in the URI. The Page will be reloaded with the new uri.
  277. * @param {string} paramName - The key (name) to identify the query parameter
  278. * @param {string} paramValue - The new or updated value of the query parameter
  279. */
  280. var setUriQueryParam = function(paramName, paramValue) {
  281. var uri = window.location.href;
  282. var re = new RegExp("([?&])" + paramName + "=.*?(&|$)", "i");
  283. var separator = uri.indexOf('?') !== -1 ? "&" : "?";
  284. if (uri.match(re)) {
  285. uri = uri.replace(re, '$1' + paramName + "=" + paramValue + '$2');
  286. }
  287. else {
  288. uri = uri + separator + paramName + "=" + paramValue;
  289. }
  290. window.location.href = uri;
  291. };
  292. return {
  293. uri : {
  294. getQueryParamValue : getUriQueryParamValue,
  295. setQueryParamValue : setUriQueryParam
  296. }
  297. };
  298. }();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址