如何替换html textarea中的选定文本?
mardagz 至 [b]mardagz[\b]
这是我的代码,但不会工作..
function getSelectedText(){
if(window.getSelection){
return window.getSelection().toString();
}
else if(document.getSelection){
return document.getSelection();
}
else if(document.selection){
return document.selection.createRange().text;
}}
function bbrep(start, end){
$("#pPost").val($("#pPost").val().replace(getSelectedText(), start + getSelectedText() + end))};
Run Code Online (Sandbox Code Playgroud)
键
$("#bbbold").click(function(){
bbrep("[b]", "[/b]");
return false;
})
Run Code Online (Sandbox Code Playgroud)
有谁有这个想法?:)
Dan*_*iel 12
这是一个完整的测试和工作示例
function getSelectedText() {
var len =$("#pPost").val().length;
var start = $("#pPost")[0].selectionStart;
var end = $("#pPost")[0].selectionEnd;
var sel = $("#pPost").val().substring(start, end);
return sel;
}
function bbrep(start, end){
var tmpVal = getSelectedText();
$("#pPost").val($("#pPost").val().replace(tmpVal, start + tmpVal + end));
Run Code Online (Sandbox Code Playgroud)
}
$("#bbbold").click(function(){
bbrep("[b]", "[/b]");
return false;
Run Code Online (Sandbox Code Playgroud)
})
这是html代码snipet
<textarea rows="2" cols="20" id="pPost">mardagz
</textarea>
<input type="button" id="bbbold" value="clcikMe" />
Run Code Online (Sandbox Code Playgroud)
这是一个jsfiddle例子,我只是做了"它的工作"
这需要该指数考虑过另一种的jsfiddle,jsfiddle.net/SU7wd/58 < -工作是在这个第二一太
| 归档时间: |
|
| 查看次数: |
4237 次 |
| 最近记录: |