Redactor - 未捕获的TypeError:无法读取未定义的属性“insert”

时间:2015-06-13 12:57:55

标签: javascript jquery redactor

我正在尝试在我的项目中使用Redactor。我想在点击按钮时向编辑器添加一些文本。

API说你可以这样做:

function insertHtml()
{
    var html = '<h3>INSERTED</h3>';
    $('#redactor').redactor('insert.html', html);
}

好吧,我的代码看起来像这样:

<button onclick="insertHtml();">Insert</button>

<script type="text/javascript">
    function insertHtml()
    {
        var html = '<h3>INSERTED</h3>';
        $('.redactor-box').redactor('insert.html', html);
    }
</script>

Redactor编辑:

<div class="redactor-box">
<ul class="redactor-toolbar" id="redactor-toolbar-0" style=
"position: relative; width: auto; top: 0px; left: 0px; visibility: visible;">
<li>
        <a class="re-icon re-bold" href="#" rel="bold" tabindex="-1"></a>
    </li>

    <li>
        <a class="re-icon re-italic" href="#" rel="italic" tabindex=
        "-1"></a>
    </li>

    <li>
        <a class="re-icon re-deleted" href="#" rel="deleted" tabindex=
        "-1"></a>
    </li>

    <li>
        <a class="re-icon re-unorderedlist" href="#" rel="unorderedlist"
        tabindex="-1"></a>
    </li>

    <li>
        <a class="re-icon re-orderedlist" href="#" rel="orderedlist"
        tabindex="-1"></a>
    </li>

    <li>
        <a class="re-icon re-outdent" href="#" rel="outdent" tabindex=
        "-1"></a>
    </li>

    <li>
        <a class="re-icon re-indent" href="#" rel="indent" tabindex=
        "-1"></a>
    </li>

    <li>
        <a class="re-icon re-image" href="#" rel="image" tabindex="-1"></a>
    </li>

    <li>
        <a class="re-icon re-file" href="#" rel="file" tabindex="-1"></a>
    </li>

    <li>
        <a class="re-icon re-link" href="#" rel="link" tabindex="-1"></a>
    </li>

    <li>
        <a class="re-icon re-horizontalrule" href="#" rel="horizontalrule"
        tabindex="-1"></a>
    </li>

    <li>
        <a class="re-icon re-mathematik redactor-btn-image" href="#" rel=
        "mathematik" tabindex="-1"></a>
    </li>
</ul>

<div class="redactor-editor redactor-placeholder" contenteditable="true"
dir="ltr" style="min-height: 300px;">
    <p>​</p>
</div>
<textarea class="redactor-box" cols="40" data-redactor-options=
"{&quot;lang&quot;: &quot;en&quot;, &quot;fileUpload&quot;: &quot;/redactor/upload/file/&quot;, &quot;imageUpload&quot;: &quot;/redactor/upload/image/&quot;}"
dir="ltr" id="id_body" name="body" placeholder="Vsebina vprašanja.." rows=
"10" style="display: none;">

我得到的错误:

Uncaught TypeError: Cannot read property 'insert' of undefined(anonymous function) @ redactor.js:47n.extend.each @ jquery-2.1.4.min.js:2n.fn.n.each @ jquery-2.1.4.min.js:2$.fn.redactor @ redactor.js:39insertHtml @ (index):366onclick @ (index):205

我做错了什么?

3 个答案:

答案 0 :(得分:0)

我将假设它,因为该示例使用jquery id查找,它返回单个元素,并且您的实现使用jquery类查找,它返回一个元素数组。

使用:$('.redactor-box')[0]或将相关元素的类更改为ID,然后使用$('#redactor-box')

答案 1 :(得分:0)

您必须在调用API方法之前初始化编辑器

这应该做的工作:

using System.Drawing;

namespace stuff
{
    class Program
    {
        static void Main(string[] args)
        {

            Bitmap pImage = new Bitmap(@"C:\Users\...\Desktop\test.jpg");
            Bitmap pModified = new Bitmap(pImage.Width, pImage.Height);

            Color tintColor = Color.FromArgb(255, 0, 0);

            for (int x = 0; x < pImage.Width; x++)
            {
                for (int y = 0; y < pImage.Height; y++)
                {
                    //Calculate the new color
                    var oldColor = pImage.GetPixel(x, y);
                    byte red =(byte)(256.0f * (oldColor.R / 256.0f) * (tintColor.R / 256.0f));
                    byte blue = (byte)(256.0f * (oldColor.B / 256.0f) * (tintColor.B / 256.0f));
                    byte green = (byte)(256.0f * (oldColor.G / 256.0f) * (tintColor.G / 256.0f));

                    Color newColor = Color.FromArgb(red, blue, green);
                    pModified.SetPixel(x, y, newColor);
                }
            }
            pModified.Save(@"C:\Users\...\Desktop\tint.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
        }
    }
}

答案 2 :(得分:0)

标记中有2个元素具有相同的类redactor-box

<div class="redactor-box">

<textarea class="redactor-box"

尝试将textarea的类更改为redactor,然后在此类上调用Redactor API方法:

$('.redactor').redactor('insert.html', html);

但是如果你在1页上有几个编辑器,那么所有这些都会插入这个html,所以最好使用id或更具体的类。

另请注意,您只能在初始化Redactor后调用Redactor API方法,因此您应首先调用:

$('.redactor').redactor();

(可能你已经这样做了,明确提到它以防万一)