如何在jQuery中替换我选择的文本?

时间:2011-12-06 18:50:49

标签: jquery

如何替换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))};

$("#bbbold").click(function(){
          bbrep("[b]", "[/b]");
          return false;      
 })

有人对此有所了解吗? :)

5 个答案:

答案 0 :(得分: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));

}

$("#bbbold").click(function(){
      bbrep("[b]", "[/b]");
      return false;      

})

这是html代码snipet

<textarea rows="2" cols="20" id="pPost">mardagz
</textarea>
<input type="button" id="bbbold" value="clcikMe" />

这是一个jsfiddle例子,我刚做"it works"


另一个将索引考虑在内的jsfiddle,jsfiddle.net/SU7wd/58&lt; - 也适用于第二个 a

答案 1 :(得分:2)

以防万一有人来到这里,我将根据上述内容分享我在一些研究后发现的解决方案。我正在寻找一种解决方案,它可以取代我要求替换的内容(而不是textarea中某处的子串),也可以将焦点留在插入文本的末尾。

以下是我的解决方案:

function text_with_insert(str, index, value) {
    return str.substr(0, index) + value + str.substr(index);
}    

function surround_sel_text( str_start, str_end ) {
  var el = $("#pPost");
  var focus_idx = el[0].selectionEnd + str_start.length + str_end.length;

  var text_with_end_insert = text_with_insert( el.val(), el[0].selectionEnd, str_end );
  el.val( text_with_insert( text_with_end_insert, el[0].selectionStart, str_start ) );
  el[0].selectionStart = focus_idx;
  el[0].selectionEnd = focus_idx;
}

$("#bbbold").mousedown(function() {
      surround_sel_text( '[b]', '[/b]' );
      return false;      
});

答案 2 :(得分:1)

这有效:

function bbrep(start, end) {
    var str = $("#pPost").val();
    var word = "bar";//getSelectedText();
    var re = new RegExp("(\\b" + word + "\\b)", "ig");
    $("#pPost").val(str.replace(re, start + "$1" + end));
};

但是我必须对字符串进行硬编码,因为getSelectedText()方法不会返回任何内容。

编辑:

不使用.click,而是使用.mousedown

.click发生时,不再选择文字。

答案 3 :(得分:1)

好的我必须重建这个才能使用我的asp.net控件。想想它会变得如此简单。这个将替换所选择的内容,而不是其他内容中的第一个单词。

请确保为其提供文本框的ID。

您可以像这样调用函数makeBBCode(“[b]”,“[/ b]”)。我使用OnClient点击一个asp图像按钮来调用它。 [例如:OnClientClick ='makeBBCode(“[b]”,“[/ b]”)']

    String.prototype.insertAt = function (index, string) {
        return this.substr(0, index) + string + this.substr(index);
    }

    function makeBBCode(start, end) {
        var txtBox = document.getElementById("Object_Id_Here")
        txtBox.value = txtBox.value.insertAt(txtBox.selectionEnd, end).insertAt(txtBox.selectionStart, start);
    }

答案 4 :(得分:0)

您可以使用:

https://github.com/timdown/rangyinputs

只需调用:replaceSelectedText