document.selection 对象含义及简单应用实例
来源:互联网 发布:如何才能进去sci数据库 编辑:程序博客网 时间:2024/05/18 01:07
document.selection 只有 IE 支持,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下有效)
<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、查看选择的内容
<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、清除选中的内容
<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、通过脚本选择内容
<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>
selection 对象代表了当前激活选中区,即高亮文本块,或文档中用户可执行某些操作的其它元素。selection 对象的典型用途是作为用户的输入,以便识别正在对文档的哪一部分正在处理,或者作为某一操作的结果输出给用户。
用户和脚本都可以创建选中区。用户创建选中区的办法是拖曳文档的一部分。脚本创建选中区的办法是在文本区域或类似对象上调用 select 方法。要获取当前选中区,请对 document 对象应用 selection关键字。要对选中区执行操作,请先用 createRange() 方法从选中区创建一个文本区域对象。document.selection.createRange() 根据当前文字选择返回 TextRange 对象,或根据控件选择返回 ControlRange 对象。配合 execCommand,在 HTML 编辑器中很有用,比如:文字加粗、斜体、复制、粘贴、创建超链接等。selection.type 选中内容的类型。 //document.selection.createRange().parentElement().name。
一个文档同一时间只能有一个选中区。选中区的类型决定了其中为空或者包含文本或元素块。尽管空的选中区不包含任何内容,你仍然可以用它作为文档中的位置标志。
1、一个简单的例子(对选中的文本执行加粗命令 , 该脚本只在IE下有效)
<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、查看选择的内容
<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、清除选中的内容
<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、通过脚本选择内容
<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对象的介绍
- 关于document.selection对象的介绍
- document.selection
- document.selection
- document.selection
- TextRange对象和selection对象实例
- document.selection.createRange方法----获取选择对象文本
- 使用document.selection 简单判断浏览器是否为ie核心
- Window及document对象
- ERP含义及实例
- Ajax简单应用示例: XMLHttpRequest对象实例化方式及调用
- document.getSelection,document.selection.createRange
- document.getSelection,document.selection.createRange
- JAVA中运算符含义及简单应用
- document.selection.createRange方法
- document.selection.createRange
- Document.selection相应练习
- myeclipse的编译和运行环境不一样引发webService异常
- 使用 HTML5 数据库和离线功能
- response header 设置 限速
- NYOJ 108 士兵杀敌(一)
- python的包管理系统
- document.selection 对象含义及简单应用实例
- 源码资料下载学习地址
- cocos2d-x v2.2的那些坑
- HTML默认样式表CSS属性
- 在ios真机上运行cocos2d-html5-2.2自带的HelloHTML5World
- Burpsuite导出log配合Sqlmap批量扫描注入点
- IOS 简单内存管理
- JS跳转
- ExtJs表单验证的方法总结