document.selection对象的介绍
来源:互联网 发布:域名不续费会怎么样 编辑:程序博客网 时间:2024/06/05 02:29
http://msdn.microsoft.com/en-us/library/ie/ms535872%28v=vs.85%29.aspx
window.getSelection()也只有FireFox和Safari支持,都不是标准语法。
selection 对象代表了当前激活选中区,即高亮文本块,或文档中用户可执行某些操作的其它元素。selection 对象的典型用途是作为用户的输入,以便识别正在对文档的哪一部分正在处理,或者作为某一操作的结果输出给用户。
用户和脚本都可以创建选中区。用户创建选中区的办法是拖曳文档的一部分。脚本创建选中区的办法是在文本区域或类似对象上调用 select 方法。要获取当前选中区,请对document对象应用selection关键字。要对选中区执行操作,请先用createRange方法从选中区创建一个文本区域对象。document.selection.createRange() 根据当前文字选择返回 TextRange 对象,或根据控件选择返回 ControlRange 对象。配合 execCommand,在 HTML 编辑器中很有用,比如:文字加粗、斜体、复制、粘贴、创建超链接等。selection.type选中内容的类型。 //document.selection.createRange().parentElement().name。
一个文档同一时间只能有一个选中区。选中区的类型决定了其中为空或者包含文本或元素块。尽管空的选中区不包含任何内容,你仍然可以用它作为文档中的位置标志。
1、一个简单的例子(对选中的文本执行加粗命令 , 该脚本只在IE下有效)
XML/HTML代码
<html>
<head>
<title>a test for selection object</title>
</head>
<body>
<script language='javascript'>
function test()
{
var textSelection = document.selection.createRange();
textSelection.execCommand('Bold');
}
</script>
<div onmouseup = "javascript:test();">select me.... , I will be bold..</div>
</body>
</html>
2、查看选择的内容
XML/HTML代码
<html>
<head>
<title>a test for selection object</title>
</head>
<body>
<script language='javascript'>
function showSelectContent(isIncludingHtmlTag)
{
var textSelection = document.selection.createRange();
if (isIncludingHtmlTag) alert(textSelection.htmlText);
else alert(textSelection.text);
return false;
}
</script>
<a href='#' onmouseup='javascript:showSelectContent(0);'>
select me. show selecting text
<br />
<a href='#' onmouseup='javascript:showSelectContent(1);'>
select me. show selecting htmlText
</body>
</html>
3、清除选中的内容
XML/HTML代码 <html>
<head>
<title>a test for selection object</title>
</head>
<body>
<script language='javascript'>
function clearSelectionContent()
{
document.selection.clear();
}
</script>
<form>
<textarea cols=20 rows=5>
please select the whole me or parts of me , if you want to delete me.
</textarea>
<button type='button' onclick='javascript:clearSelectionContent();'>
delete selected contents
</buton>
</form>
</body>
</html>
4、通过脚本选择内容
XML/HTML代码
<html>
<head>
<title>a test for selection object</title>
</head>
<body>
<script language='javascript'>
function selectionContentByScript()
{
var t=document.getElementById("test");
var o=t.createTextRange();
//o.moveStart("character",2);
o.select();
}
</script>
<form>
<input id='test' type='text' value='will be selected' />
<br />
<input type='button' onclick='javascript:selectionContentByScript();' value='select the text box value' />
</form>
</body>
</html>
0 0
- document.selection对象的介绍
- 关于document.selection对象的介绍
- doc.selection对象介绍
- ff下的document.selection
- document.selection
- document.selection
- document.selection
- IFrame 系列4 ---- document.selection 全方位兼容解析以及TextRange[createTextRange,createRange]对象的深入解析
- document对象execCommand的命令参数介绍
- document对象execCommand的命令参数介绍
- document.selection 的 createRange()方法汇总
- DHTML之-----document.selection 的 createRange
- document.selection 和getSelection 的demo
- Document对象介绍
- javascript Document 对象介绍
- document.selection 对象含义及简单应用实例
- document.selection.createRange方法----获取选择对象文本
- document.getSelection,document.selection.createRange
- oracle数据库导入导出命令!
- iOS开发:统计xcode代码的行数
- LLVM Metadata 介绍
- js 触摸事件
- Vim下编辑二进制文件方法
- document.selection对象的介绍
- 建立内核源码树 --部分为转载
- OJ_1068
- Lumanager网站突然间全都不见了
- 解决div被flash遮挡的问题
- 小小投资,巨大回报
- C++ map 模板
- sed命令基本语法笔记整理
- 对url中的参数进行URLEncode编码的实现