document.getSelection相关的小例子

<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;//代码可以捕获到选中的网页中的纯文//本内容(不含HTML标签)
  txt=document.selection.createRange().htmlText//HTML标签
  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)