<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>
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>
本文介绍了一个简单的网页文本选中插件实现方案。该插件利用 JavaScript 的不同方法来获取用户当前在页面上选择的文本内容,并将其展示在一个表单区域中。这对于调试或者需要收集用户感兴趣部分的应用场景非常有用。
293

被折叠的 条评论
为什么被折叠?



