最近想学着写一下markdow编辑器。但是在实现加粗、斜体等类似功能的时候不知道该怎么弄了。
请问一下segmentfault的类似于加粗,斜体的编辑功能是如何实现的呢?
就是怎么获取到选取的文字,然后点击加粗按钮后,用使用markdown语法的内容替换原本选中的文字并且还保持文字的选中状态呢?
其实就是给选中的文字套了一层标签,然后给这层标签设置样式
html
标签啊 css样式 啊
<p class="answer fmt" data-id="1020000007020989">
<p><code>html</code>标签啊 <strong>css</strong>样式 啊</p>
</p>
刚弄了 js,就已经采纳了~
还是贴了吧
function $$(id){
return document.getElementById(id);
}
function textSelect(textBox,start,end){
if(textBox.setSelectionRange){
textBox.setSelectionRange(start,end);
}else if(textBox.createTextRange){
var rang = textBox.createTextRange();
rang.collapse(true);
rang.moveStart('character',start);
rang.moveEnd('character',end-start);
rang.select();
}
textBox.focus();
}
textSelect($$("myEditor"),5,15); //SF 的编辑区id 是"myEditor"
SF 已开源
https://github.com/SegmentFau...