选择某元素内的文本


var autoSelect = function (el) {

  if (/textarea/i.test(el.tagName) || (/input/i.test(el.tagName) && /text/i.test(el.type))) {

    el.select();

  } else if (!!window.getSelection) { // FF, Safari, Chrome, Opera

    var sel = window.getSelection();

    var range = document.createRange();

    range.selectNodeContents(el);

    sel.removeAllRanges();

    sel.addRange(range);

  } else if (!!document.selection) { // IE

    document.selection.empty();

    var range = document.body.createTextRange();

    range.moveToElementText(el);

    range.select();

  }

};

你可能感兴趣的:(选择)