最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set content in Redactor textarea - Stack Overflow

programmeradmin3浏览0评论

I want to use jQuery to grab the id from a hyperlink.

<a class="aDirectReply" id="user1234">Respond to user1234</a>

then set the content of a redactor textarea

<textarea rows="10" class="form-control" id="redactor_content" name="content"></textarea>

Here is my jQuery


    $(document).ready(function()        
        {
            var buttons = ['formatting', '|', 'bold', 'italic', 'fullscreen', '|', 'button1'];
            $('#redactor_content').redactor({
                focus: true,
                buttons: buttons
            });
            
            $('a.aDirectReply').on("click",function(e){
                var username = $(this).attr("id");
                $('#redactor_content').redactor('set', '@' + username); // doesn't work

                ....

I've tried everything I can find on StackOverflow, including this: Textarea editor Redactor. Insert value with jQuery But nothing has worked so far.

The docs give an example:

let app = Redactor('#entry');

// insert content from outside scripts
app.editor.insertContent({ html: '<p>Hello world!</p>' });

// insert content in the plugin method
this.app.editor.insertContent({ html: '<p>Hello world!</p>' });

but all their examples result in the error "Redactor is not defined"

I want to use jQuery to grab the id from a hyperlink.

<a class="aDirectReply" id="user1234">Respond to user1234</a>

then set the content of a redactor textarea

<textarea rows="10" class="form-control" id="redactor_content" name="content"></textarea>

Here is my jQuery


    $(document).ready(function()        
        {
            var buttons = ['formatting', '|', 'bold', 'italic', 'fullscreen', '|', 'button1'];
            $('#redactor_content').redactor({
                focus: true,
                buttons: buttons
            });
            
            $('a.aDirectReply').on("click",function(e){
                var username = $(this).attr("id");
                $('#redactor_content').redactor('set', '@' + username); // doesn't work

                ....

I've tried everything I can find on StackOverflow, including this: Textarea editor Redactor. Insert value with jQuery But nothing has worked so far.

The docs give an example:

let app = Redactor('#entry');

// insert content from outside scripts
app.editor.insertContent({ html: '<p>Hello world!</p>' });

// insert content in the plugin method
this.app.editor.insertContent({ html: '<p>Hello world!</p>' });

but all their examples result in the error "Redactor is not defined"

Share Improve this question edited Apr 4 at 20:07 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Mar 22 at 1:26 user460114user460114 1,8954 gold badges31 silver badges56 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I found the only thing that worked. First destroy, then recreate with the new content.

$('#redactor_content').destroyEditor();
$('#redactor_content').redactor().setCode('@' + username);
发布评论

评论列表(0)

  1. 暂无评论