[Javascript]document.getSelection,document.selecti

<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
 

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏