select.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.select = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. function select(element) {
  3. var selectedText;
  4. if (element.nodeName === 'SELECT') {
  5. element.focus();
  6. selectedText = element.value;
  7. }
  8. else if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
  9. var isReadOnly = element.hasAttribute('readonly');
  10. if (!isReadOnly) {
  11. element.setAttribute('readonly', '');
  12. }
  13. element.select();
  14. element.setSelectionRange(0, element.value.length);
  15. if (!isReadOnly) {
  16. element.removeAttribute('readonly');
  17. }
  18. selectedText = element.value;
  19. }
  20. else {
  21. if (element.hasAttribute('contenteditable')) {
  22. element.focus();
  23. }
  24. var selection = window.getSelection();
  25. var range = document.createRange();
  26. range.selectNodeContents(element);
  27. selection.removeAllRanges();
  28. selection.addRange(range);
  29. selectedText = selection.toString();
  30. }
  31. return selectedText;
  32. }
  33. module.exports = select;
  34. },{}]},{},[1])(1)
  35. });