document.getSelection,document.selection.createRange
2006-09-28 21:25
344 查看
<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
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
相关文章推荐
- document.selection.createRange
- document.selection.createRange方法与实例
- document.selection.createRange方法(转)
- document.selection.createRange方法----获取选择对象文本
- document.selection.createRange方法
- document.getSelection,document.selection.createRange
- 【js】document.selection.createRange().text
- document.selection.createRange方法----获取选择对象文本
- document.selection.createRange方法
- document.selection.createRange方法
- document.selection.createRange()
- document.selection.createRange方法与实例
- document.selection.createRange方法
- IE中iframe和document.selection.createRange导致跨域
- document.selection.createRange方法
- 获取 Textarea 元素当前的光标位置及document.selection.createRange()资料
- 获取Textarea 元素当前的光标位置及document.selection.createRange()资料
- document.selection.createRange() (转)
- document.getSelection,document.selection.createRange
- 获取Textarea 元素当前的光标位置及document.selection.createRange()资料