我想做一個jQuery文本編輯器,我做了一些Google搜索,並獲得了一些工作的基本功能是,當用戶突出顯示textarea中的文本時,它將在前面添加[b], [/ b]在文本的結尾,但是它的作用是,如果用戶再次單擊相同的按鈕,它將刪除所有輸入的數據,並且只將突出顯示的文本與[b] [/ b]放在一起,代碼如下:jquery文本編輯器
$('#bold').mousedown(function(e) {
var txt = '';
if (window.getSelection){
txt = window.getSelection();
}
else if(document.getSelection){
txt = document.getSelection();
}
else if(document.selection){
txt = document.selection.createRange().text;
}
else return;
txt = "[b]"+txt+"[/b]";
$('#reply').val(txt);
});
HTML
<input type="button" value="Bold" id="bold" />
<form action="" method="get">
<textarea name="reply" id="reply" cols="100" rows="30">Test</textarea>
</form>
爲什麼重新創建輪子? –
我正在開發一個項目,當它賣掉時,我不想讓第三方的東西包含在內 –