jquery附加到索引位置
jquery appending to a index location
我正在尝试制作一个rtf风格的文本框。当我提醒选择时,我的get选择有效。我正试图让它在选择之前和之后附加一个。但我所尝试的一切都只是不断地附加到html的末尾。
我的Html是非常简单的
<p id="textblock" contenteditable="true">
This is just a simple paragraph tag that is content editable. the plan is to make it responsive to RTF controls like bold, italics, and other options.
</p>
<button id="boldButton">BOLD</button>
<button id="viewButton">ViewHtml</button>
<button id="selection">ViewSelection</button>
我的jquery是
$( "#boldButton" ).click(function() {
Boldify();
});
$( "#viewButton" ).click(function() {
alert( $("#textblock").html() );
});
$( "#selection" ).click(function() {
alert( document.getSelection().toString());
});
function Boldify(){
var startIndex = document.getSelection().getRangeAt(0).startOffset;
var endIndex = document.getSelection().getRangeAt(0).endOffset;
$("<strong>").appendTo("#textblock").index(window.getSelection());
$("</strong>").appendTo("#textblock").index(endIndex);
}
这是我的小提琴。http://jsfiddle.net/3oaup7gn/我觉得我很亲近。但我好像错过了什么。我尝试过使用静态索引和getSelection()索引,但它总是做同样的事情。
我的朋友,你必须使用execCommand(),只需将其添加到Boldify()函数中即可。
document.execCommand('bold', false, true);
阅读整个API:https://developer.mozilla.org/en-US/docs/Web/API/document.execCommand
相关文章:
- 在从索引位置返回的字符串中查找空白
- 需要呈现MongoDB查询返回结果的特定索引/位置
- 有没有办法得到引导3旋转木马的索引位置
- 点击JQuery/HTML5显示元素的索引/位置
- Javascript i(index,iterator):如果用作变量,它将始终被解释为索引位置
- 获取所选元素的位置,而不是 DOM 中元素的索引
- Javascript 返回数组中“匹配”数组的位置索引
- 动态 jquery ui 可排序块的索引/位置
- Windows 8 应用程序中的索引数据库位置
- 如何删除字符串中的索引位置范围
- 工具提示 z 索引位置或其他内容
- jquery附加到索引位置
- 特殊字符的javascript验证's索引/位置
- Phonegap:在目录中导航,索引位置不正确
- 如何在AngularJS中保存对象的JSON索引位置,而不是对象本身
- 将数组元素按索引位置相乘
- 返回匿名函数数组的索引位置
- 哪个值是javascript数组.indexOf作为索引位置
- 如何通过文本搜索返回JSON数组中项目的索引位置?
- 按索引位置解析json对象数组