2010-02-24 72 views
0

我覺得這是一個簡單的問題,但我無法找到答案如何飲用YUI編輯事件

我有一個Rails應用程序,我使用jQuery和YUI的富文本編輯器,我想寫一些javascript/jquery到有一個最小文本長度計數器(即你有240個字符左)。我有一個簡單的文本方面的工作:

jQuery.fn.populateCounter = function() { 
    var text_length = $(this).val().length; 
    var counter = $(this).siblings('p.counter'); 
    if (text_length == 0) { 
     counter.text('Enter at least 15 characters'); 
    } 
    else if (text_length < 15) { 
     var left = 15 - text_length 
     counter.text(left + ' characters to go'); 
    } 
    else if (text_length <= 600) { 
     var remaining = 600 - $(this).val().length; 
     counter.text(remaining + ' characters left'); 
    } 
    else { 
     var to_many = $(this).val().length - 600; 
     counter.text(to_many + ' characters to many'); 
    } 
    return this; 
} 

$('.new_comment textarea').live('keyup',function() { 
    $(this).populateCounter(); 
}); 

但我不清楚如何與YUI編輯器做到這一點(我如何消費有活動,如何獲得內容的長度是多少?) 。謝謝你的幫助!

- JT

回答

1

看一看的YAHOO.widget.SimpleEditor事件。一旦你創建了一個編輯器(也許this是一個開始的好地方),你可以使用editorKeyPress事件觸發你的文本長度檢查。

myEditor.subscribe('editorKeyPress', function() { 
    var textLength = myEditor.toString().length; 
    // .... do checks 
}); 
+0

很酷。謝謝加文 – Jonathan 2010-03-01 15:02:49