简体   繁体   English

bootstrap-wysiwyg 获取实例未定义

[英]bootstrap-wysiwyg get instance is undefined

I'm following their guide in Github on how to grab the wysihtml5 instance based on the textarea ID or class you put it on.我正在遵循他们在 Github 中的指南,了解如何根据您放置的 textarea ID 或类获取 wysihtml5 实例。 This JSFiddle shows the error I'm running into:这个 JSFiddle 显示了我遇到的错误:

http://jsfiddle.net/ez3ajvaf/2/ http://jsfiddle.net/ez3ajvaf/2/

This is my HTML:这是我的 HTML:

<span class="footer-btn quote" rel=".text-editor" data-text="body-1"><span class="icon"><i class="fa fa-quote-left"></i></span> Quote</span>

<textarea name="body" class="text-editor text-countdown" placeholder="Please allow up to 2,000 characters" maxlength="2000" data-rel="countdown" id="body" required></textarea>

<div class="countdown-wrapper">
    <span id="countdown" class="countdown"></span>
</div>

This is my Javascript:这是我的Javascript:

$('.text-editor').wysihtml5({
    toolbar: {
        "font-styles": true, //Font styling, e.g. h1, h2, etc. Default true
        "emphasis": true, //Italics, bold, etc. Default true
        "lists": true, //(Un)ordered lists, e.g. Bullets, Numbers. Default true
        "html": false, //Button which allows you to edit the generated HTML. Default false
        "link": true, //Button to insert a link. Default true
        "image": true, //Button to insert an image. Default true,
        "color": false, //Button to change color of font  
        "blockquote": true, //Blockquote
    }
});

 $('.quote').on('click', function() {

     console.log('clicked');

     var textarea = $(this).attr('rel');

     console.log($(textarea).data("wysihtml5").editor);

     $(textarea).data("wysihtml5").editor.html('hello');

 });

The error I'm receiving is我收到的错误是

Uncaught TypeError: Cannot read property 'html' of undefined未捕获的类型错误:无法读取未定义的属性“html”

Under their github https://github.com/bootstrap-wysiwyg/bootstrap3-wysiwyg if you scroll down to "You can access the wysihtml5 editor object like this:", it shows this exact code based on their textarea ID they initiated wysihtml5 on.在他们的 github https://github.com/bootstrap-wysiwyg/bootstrap3-wysiwyg 下,如果你向下滚动到“你可以像这样访问 wysihtml5 编辑器对象:”,它会根据他们启动 wysihtml5 的 textarea ID 显示这个确切的代码. My text editor loads fine and it works great, but I can't seem to grab the instance.我的文本编辑器加载良好,效果很好,但我似乎无法获取实例。 data-wysihtml5 doesn't appear to be added on as an attribute when I initiate the code to begin with.当我启动代码时, data-wysihtml5似乎没有作为属性添加。

I'm trying to get when you click "Quote" it will quote the post (which I can add in later to grab that context) into the text editor.我试图让当您单击“引用”时,它会将帖子(我可以稍后添加以获取该上下文)引用到文本编辑器中。 Thank you for any help or guidance!感谢您的任何帮助或指导!

I had the same problem with update wysihtml5 editor and resolved it with this:我在更新 wysihtml5 编辑器时遇到了同样的问题,并通过以下方式解决了它:

$('.wysihtml5-sandbox').contents().find('body').html('<b>New text</a>');

I know, it's not best solution, but only this works for me.我知道,这不是最好的解决方案,但只有这对我有用。 If you have better solution, please let me know.如果您有更好的解决方案,请告诉我。

The fiddle is not working anymore, probably some dependencies issues.小提琴不再工作,可能是一些依赖问题。 The problem might have been in how the contents of textarea, or missing dependencies, who knows.问题可能出在 textarea 的内容如何,​​或者缺少依赖项,谁知道。

In any case this works:在任何情况下,这都有效:

$('.textarea').data('wysihtml5').editor

see: http://jsfiddle.net/estani/pfm2bx6j/1/见: http : //jsfiddle.net/estani/pfm2bx6j/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM