2013-11-04 77 views
1

我試圖添加一個按鈕來更改Redactor JS中的字體大小,但它不起作用。這裏是我的代碼intialise主編:添加字體大小爲Redactor JS

$('#redactor_content').redactor({ 
         buttons: buttons, 
         buttonsCustom: { 
          superscript: { 
           title: 'Superscript', 
           callback: function(obj, event, key) { 
            obj.execCommand('superscript') 
           } 
          }, 
          subscript: { 
           title: 'Subscript', 
           callback: function(obj, event, key) { 
            obj.execCommand('subscript') 
           } 
          } 
         }, 
         plugins: ['fontsize'] 
        }); 

,這是我的插件:

RedactorPlugins.fontsize = { 
init: function() 
{ 
    var fonts = [10, 11, 12, 14, 16, 18, 20, 24, 28, 30]; 
    var that = this; 
    var dropdown = {}; 

    $.each(fonts, function(i, s) 
    { 
     dropdown['s' + i] = { title: s + 'px', callback: function() { that.setFontsize(s); } }; 
    }); 

    dropdown['remove'] = { title: 'Remove font size', callback: function() { that.resetFontsize(); } }; 

    this.buttonAdd('fontsize', 'Change font size', false, dropdown); 
}, 
setFontsize: function(size) 
{ 
    this.inlineSetStyle('font-size', size + 'px'); 
}, 
resetFontsize: function() 
{ 
    this.inlineRemoveStyle('font-size'); 
} 
}; 

但沒有出現在工具欄中。任何想法?我做錯了什麼嗎?

+0

你的'buttons'變量是什麼?假設它是按鈕字符串數組,是否給它添加了「Superscript」和「Subscript」? – MForMarlon

回答

1

嘗試yuour插件

if (!RedactorPlugins) var RedactorPlugins = {}; 

RedactorPlugins.fontsize = { 
    init: function(){ 
     var fonts = [10, 11, 12, 14, 16, 18, 20, 24, 28, 30]; 
     var that = this; 
     var dropdown = {}; 

     $.each(fonts, function(i, s){ 
      dropdown['s' + i] = { title: s + 'px', callback: function() { that.setFontsize(s); } }; 
     }); 

     dropdown['remove'] = { title: 'Remove font size', callback: function() { that.resetFontsize(); } }; 
     this.buttonAdd('fontsize', 'Change font size', false, dropdown); 
    }, 
    setFontsize: function(size){ 
     this.inlineSetStyle('font-size', size + 'px'); 
    }, 
    resetFontsize: function(){ 
     this.inlineRemoveStyle('font-size'); 
    } 
}; 

之前添加該代碼同時刪除任何額外的代碼,以確保您遇到有問題的代碼中的問題。 也許還會將它粘在一個文件準備好的事件中

<script type="text/javascript"> 
$(document).ready(
    function() 
    { 
     $('#redactor_content').redactor({ 
      focus: true, 
      plugins: ['fontsize'] 
     }); 
    } 
); 
</script> 

代碼適用於我。

+0

也適用於我。謝謝! – Codel

0

您可以使用官方的「字號」插件,如果你想:https://github.com/johnsardine/redactor-plugins

也有其他新的插件可供選擇:

fontcolor.js, fontsize.js and fontfamily.js 

如果包含,你可以使用它們像這樣:

elem.redactor({ 
     plugins: ['fontcolor', 'fontsize', 'fontfamily'] 
});