document.getSelection,document.selection.createRange

<script>
function getSel()
{
      var txt = '';
      var foundIn = '';
      if(window.getSelection)
      {
          txt = window.getSelection();
          foundIn = 'window.getSelection()';
      }else if(document.getSelection)
      {
          txt = document.getSelection();
          foundIn = 'document.getSelection()';
      }else if(document.selection)
      {
          txt = document.selection.createRange().text;
          foundIn = 'document.selection.createRange()';
      }else
          return;
      document.forms[0].selectedtext.value = 'Found in: ' + foundIn + '\n' + txt;
}
</script>
<form>
<textarea name="selectedtext" rows="5" cols="50">
</textarea><br>
<input type="button" value="Getselection" onmousedown="getSel()">
</form>

以上的代码可以捕获到选中的网页中的纯文本内容(不含HTML标签)
如果想获得包含html的内容,将document.selection.createRange().text改成document.selection.createRange().htmlText
  
posted @ 2007-05-13 11:39  永不言败  阅读(3284)  评论(2编辑  收藏  举报