开发者

jQuery fieldselection replaceSelection doesn't works for me

hi this is the simple jquery fieldselection plugin.

   /*
 * jQuery plugin: fieldSelection - v0.1.1 - last change: 2006-12-16
 * (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd
 */

(function() {

    var fieldSelection = {

        getSelection: function() {

            var e = (this.jquery) ? this[0] : this;

            return (

                /* mozilla / dom 3.0 */
                ('selectionStart' in e && function() {
                    var l = e.selectionEnd - e.selectionStart;
                    return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
                }) ||

                /* exploder */
                (document.selection && function() {

                    e.focus();

                    var r = document.selection.createRange();
                    if (r === null) {
                        return { start: 0, end: e.value.length, length: 0 }
                    }

                    var re = e.createTextRange();
                    var rc = re.duplicate();
                    re.moveToBookmark(r.getBookmark());
                    rc.setEndPoint('EndToStart', re);

                    return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
                }) ||

                /* browser not supported */
                function() { return null; }

            )();

        },

        replaceSelection: function() {

            var e = (typeof this.id == 'function') ? this.get(0) : this;
            var text = arguments[0] || '';

            return (

                /* mozilla / dom 3.0 */
                ('selectionStart' in e && function() {
                    e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
                    return this;
                }) ||

                /* exploder */
                (document.selection && function() {
                    e.focus();
                    document.selection.createRange().text = text;
                    return this;
                }) ||

                /* browser not supported */
                function() {
                    e.value += text;
                    return jQuery(e);
                }

            )();

        }

    };

    jQuery.each(fiel开发者_开发百科dSelection, function(i) { jQuery.fn[i] = this; });

})();

i can't understand why doing this the selected text in textarea element is not replaced.

$('.wysiwyg-editor-italic').live('click',function(){
   var _selection = $('.wysiwyg textarea').getSelection().text;
   var wcode = "";

   if(_selection){
       $('.wysiwyg textarea').replaceSelection("{italic}"+_selection+"{/italic}");
       console.log(_selection);
    }
    else{
        wcode = "{italic} italic {/italic}";
    }
    var wysiwyg_val = $('.wysiwyg textarea').val();

    $('.wysiwyg textarea').val("").val(wysiwyg_val+wcode);
     $('.wysiwyg-preview').html(wysiwyg_val);
    $('.wysiwyg textarea').focus();



    refresh_preview()
});

in console i can see _selection which is ok but after that is not replaced ... maybe i'm missing up somenthing?


Unfortunately, it seems to be a bug on the library.

I was also affected by this bug. Everything detects and works very well, except the replaceSelection() part. I've ended up using Rangyinputs. Someday when I have free time I will try to fix this bug on Fieldselection and pull request.


Please can you make a fiddle to show the problem? try the new version of library: https://github.com/nikolasmagno/jquery-fieldselection/

I'm sure it'll work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜