您的位置:首页 > Web前端 > JavaScript

【笔记】 《js权威指南》- 第15章 脚本化文档 - 15.10 其他文档特性

2016-04-08 10:50 507 查看
1.Doucument属性:



2.获取选中的文本:

function getSelectedText() {
if(window.getSelection)
return window.getSelection().toString();
else if ( document.selection.createRange().text;
}

//兼容ie外的浏览器获取输入文本域中的选中内容
elt.value.substring(elt.selectionStart, elt.selectionEnd);


3.可编辑的内容:

(1).通过编辑标签contenteditable属性或者编辑JS元素的contenteditable属性是元素变得可编辑;

<div id="editor" contenteditable>
CLick to edit
</div>


(2). 通过设置document对象的designMode属性为“on”;

(3). 使用文本编辑命令来编辑文本:

//参数1:指令
//参数2:建议使用false, true为浏览器提示用户输入值
//参数3:超链接url
document.execCommand("bold", false, url);

//检测浏览器是否兼容指令:
document.queryCommandSupport("bold");

//检测当前是否能使用某指令:
document.queryCommandEnabled("bold");

//获取某指令当前使用状态
document.queryCommandState("bold");

//获取某些指令相关联的值:
document.queryCommandValue("fontname");

//当前选取出现几种不同的状态:
document.queryCommandIndeterm("bold");


可能兼容的指令列表:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息