document.selection.createRange() 根据当前文字选择返回 TextRange 对象,或根据控件选择返回 ControlRange 对象。
配合 execCommand,在 HTML 编辑器中很有用,比如:文字加粗、斜体、复制、粘贴、创建超链接等。
实例一:
<textarea cols=50 rows=15>
哈哈。我们都是新生来得。大家都来相互帮助呀。这样我们才能进步,我们才能赚大钱!</textarea>
<input type=button value=选择字后点击我看看 onclick=alert(document.selection.createRange().text)>
</form>
实例二:
<body>
<textarea name="textfield" cols="50" rows="6">就是现在文本域里有一段文字,当你选种其中几个字后点击一个按钮或者链接会弹出一个对话框,对话框的信息就是你选中的文字
哪位老大能解决的呀?请多多帮忙!!!谢谢
</textarea>
<input type="button" value="showSelection" onclick="alert(document.selection.createRange().text)">
<input type="button" value="showclear" onclick="alert(document.selection.clear().text)">
<input type="button" value="showtype" onclick="alert(document.selection.type)">
<textarea name="textfield" cols="50" rows="6" onselect="alert(document.selection.createRange().text)">就是现在文本域里有一段文字,当你选种其中几个字后点击一个按钮或者链接会弹出一个对话框,对话框的信息就是你选中的文字
哪位老大能解决的呀?请多多帮忙!!!谢谢
</textarea>
</body>
实例三:选中Input中的文本
<SCRIPT LANGUAGE="JavaScript">
<!--
function test2()
{
var t=document.getElementById("test")
var o=t.createTextRange()
alert(o.text)
o.moveStart("character",2)
alert(o.text)
o.select()
}
//-->
</SCRIPT>
<input type='text' id='test' name='test'><input type=button onclick='test2()' value='test' name='test3'>
对textarea中的内容,进行选中后,加效果
<script language="JavaScript">
<!--
function bold(){
Qr=document.selection.createRange().text;
if(!Qr || document.selection.createRange().parentElement().name!='description')
{
txt=prompt('Text to be made BOLD.','');
if(txt!=null && txt!='') document.form1.description.value+=''+txt+'';
}
else{
document.selection.createRange().text=''+document.selection.createRange().text+'';
document.selection.empty();
}
}
//-->
</script>
<input type="button" value="加粗" onclick="bold();" />
<textarea name="description" style="width: 436px; height: 296px">选中我,点击加粗</textarea>
实例四:javascript捕获到选中的网页中的纯文本内容
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>鼠标取词</title>
<script>
function getSel()
{
var t=window.getSelection?window.getSelection():(document.getSelection?document.getSelection():(document.selection?document.selection.createRange().text:""))
document.forms[0].selectedtext.value = t;
}
</script></head>
<body onmouseup="getSel()">
<form>
<textarea name="selectedtext" rows="5" cols="50"></textarea>
</form>
以上的代码可以捕获到选中的网页中的纯文本内容(不含HTML标签)
如果想获得包含html的内容,将document.selection.createRange().text改成document.selection.createRange().htmlText
</body>
</html>
|
分享到:
相关推荐
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]这个不错 就是现在文本域里有一段文字,当你选种其中几个字后点击一个按钮或者链接会弹出一个对话框,对话框的信息就是你选中的文字 哪位老大能解决的呀?请...
// 对于IE浏览器,使用document.selection和createRange方法 // 对于现代浏览器(如Firefox、Chrome等),使用window.getSelection() var selectText = (document.selection && document.selection.createRange) ...
- `document.selection`:在某些浏览器中,这个对象允许获取和操作用户在文档中选定的文本,例如`document.selection.createRange()`用于创建一个选区,`document.selection.empty()`用于清除选区。 了解并熟练...
var r = document.selection.createRange(); if (r == null) { return 0; } var re = input.createTextRange(), rc = re.duplicate(); re.moveToBookmark(r.getBookmark()); rc.setEndPoint('EndToStart', ...
tion Foo() {var sel = document.selection.createRange();alert(sel.text);}--> 在JavaScript中,属性和方法是编程的核心元素,它们允许我们操作和控制网页的各个方面。本篇文章主要聚焦于JavaScript中的一些常用...
总结来说,JavaScript中的`window.getSelection()`和`document.selection.createRange().text`是获取选中文本内容的关键方法,通过适配不同浏览器的API,我们可以编写出兼容各种环境的代码,从而实现跨浏览器的文本...
在 IE9 下,如果 file 控件获得焦点,则 document.selection.createRange() 将拒绝访问。这是因为 IE 浏览器的安全机制所致。因此,只需要在 file.select() 后面加一句 file.blur() 即可。 解决方法 然而,这样做...
document.selection.createRange().selectNodeContents(textToCopy); document.selection.empty(); document.selection.createRange().execCommand('copy'); } } ``` 这段代码首先获取要复制的文本元素,然后...
var range = document.selection.createRange(); if(range.parentElement().id == textBox.id){ // ... } } } ``` 插入文本 现在,我们已经获取了光标位置,下一步就是插入文本。我们可以使用以下代码来插入...
如果`window.getSelection`不存在,那么它会回退到IE的`document.selection`方法。 Firefox(FF)和其他基于Gecko内核的浏览器使用`window.getSelection()`和`Range`对象来处理光标操作。在FF中,`window....
document.selection.createRange().selectNode(document.getElementById('yourElementId')); document.execCommand('copy'); } } ``` 在上面的代码中,`yourElementId`是你要复制内容的元素ID,函数会自动选中该...
document.selection对象通过createRange()方法来获取选中的文本范围,然后通过text属性获取选中文本的字符串。随着IE逐渐向标准靠拢,document.selection对象的支持也逐渐被弃用,但在一些老旧的IE版本中仍有使用。 ...
在JavaScript中,`Selection`对象是一个非常重要的工具,它允许我们获取或操作用户在文档中当前选择的文本。这篇博客文章“js中的selection对象使用笔记+光标定位”深入探讨了如何利用`Selection`对象来处理文本选择...
在不同的浏览器环境中,获取选中文本的方法略有不同,通常涉及到`window.getSelection()`、`document.selection`(IE特有)以及`range`对象等。 1. **`window.getSelection()`**:这是现代浏览器支持的标准方法,...
sel = document.selection.createRange(); sel.text = myValue; sel.select(); } ``` ##### B. Mozilla/Netscape支持 对于Mozilla和Netscape等基于Gecko引擎的浏览器,以及现代的Webkit和Blink引擎的浏览器(如...
然而,对于不支持这些属性的老版IE浏览器,我们需要使用`document.selection`对象和`createRange()`方法来获取光标位置。以下是一个兼容IE的实现: ```javascript if (typeof(textBox.selectionStart) == "number")...
例如,`document.all.time.value`用于获取或设置HTML元素的值,`document.selection.createRange()`用于处理用户选中的文本。 4. **光标位置处理**:`getPos(obj)`和`setCursor(ctrl, pos)`函数分别用于获取和设置...
总之,通过JavaScript的`window.getSelection()`和`document.selection.createRange().text`方法,我们可以跨浏览器地获取用户选中的文本,结合`window.open()`,可以实现在新窗口中显示这些文本的功能。这对于创建...
1. **检测浏览器支持性**:首先检查当前浏览器是否支持`document.selection`方法,因为这种方法主要适用于IE浏览器,在其他浏览器(如Chrome、Firefox)中可能不适用。 ```javascript if ((document.selection) ...