首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery cleditor插件:创建新按钮

jQuery cleditor插件:创建新按钮
EN

Stack Overflow用户
提问于 2012-03-11 23:13:28
回答 1查看 3K关注 0票数 0

使用cleditor,我尝试使用以下代码设置一个自定义按钮:

代码语言:javascript
复制
(function($) {
    $.cleditor.buttons.link_species = {
        name: "link_species",
        image: "fish.gif",
        title: "Species Link",
        command: "inserthtml",
        popupName: "link_species",
        popupClass: "cleditorPrompt",
        popupContent: "Genus: <input type='text' size='15'> Species: <input type='text' size='15'><br />Remove italics? <input type='checkbox' value='remove'>&nbsp;<input type='button' value='Ok' />",
        buttonClick: link_speciesClick
    };

  // Handle the hello button click event
  function link_speciesClick(e, data) {
    // Wire up the submit button click event
    $(data.popup).children(":button")
      .unbind("click")
      .bind("click", function(e) {

        // Get the editor
        var editor = data.editor;

        var $text = $(data.popup).find(":text"),
          genus = $text[0].value,
          species = $text[1].value;

        var slug = genus + '-' + species;
        slug = htmlEntities(slug);

        var link = '/dev/species/' + slug + '/';
        var rel = link + '?preview=true';

        var display = firstUpper(genus) + ' ' + species;

        // Get the entered name
        var html = '<a href="' + link + '" rel="' + rel + '">' + display + '</a>';

        if ( !$(data.popup).find(":checkbox").is(':checked') ) {
            html = '<em>' + html + '</em>';
        }

        // Insert some html into the document
        editor.execCommand(data.command, html, null, data.button);

        // Hide the popup and set focus back to the editor
        editor.hidePopups();
        editor.focus();
      });
  }
})(jQuery);

这是一个WordPress网站,目录结构如下所示:

/wp-content/plugins/sf-species-profile/cleditor

在那里我有所有的cleditor文件和config.js.This文件是上面的代码存储的地方。

我还有一个包含24*24 fish.gif文件的images文件夹。

由于某些原因,当我运行这段代码时,我得到以下错误:

代码语言:javascript
复制
[firefox]
a is undefined
<<myurl>>/wp-content/plugins/sf-species-profiles/cleditor/jquery.cleditor.min.js?ver=3.3.1
Line 17

[chrome]
Uncaught TypeError: Cannot read property 'length' of undefined

如果我将" image“参数更改为image:"”,则会出现与"B“相同的图像,但插件工作正常,没有错误。

有没有人知道可能出了什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-12 00:52:16

使用非最小化版本进行调试会更容易。你可以从这里获得它:http://premiumsoftware.net/cleditor/ (zip)

在最小化代码的第17行,有两个函数在代码中包含一个长度调用。处理颜色的十六进制函数中的一个。另一个是imagePath函数。

代码语言:javascript
复制
function imagesPath() {
  var cssFile = "jquery.cleditor.css",
  href = $("link[href$='" + cssFile +"']").attr("href");
  return href.substr(0, href.length - cssFile.length) + "images/";
}

如果呈现的html不包含像"“这样的行,它可能会抛出您所具有的类型的错误。(Href将是未定义的。)

对于wordpress集成,你可能会发现在使用wordpress plugin version of cleditor时更容易设置。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9656128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档