Ruby's Louvre

每天学习一点点算法

导航

选择某元素内的文本

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();
  }
};

posted on 2010-12-16 23:35  司徒正美  阅读(908)  评论(1编辑  收藏  举报