2014-09-12 37 views
3

我正在使用NicEdit Inline WYSIWYG,並嘗試瞭解如何使佔位符適用於它。佔位符在NicEdit中不起作用

我創建了一個帶有屬性佔位符的div,但是當NicEdit激活它時,顯示沒有佔位符的空白textarea。

也許有一些方法可以使它工作。據我瞭解,它將文本直接寫入div。

代碼看起來像。 (我評論顯示非textarea) http://i.imgur.com/D384B5C.png

回答

0

您可以手動添加/刪除佔位符,通過使用nicEditor事件。
focus編輯器獲得焦點時發送(IE有人在其中點擊)。
blur編輯器實例失去焦點時發送。

var editor = new nicEditor(); 
var $placeholder = '<p>Click here to add content...</p>'; 

$('[id^="textArea"]').each(function() { 
    var textAreaId = $(this).attr("id"); 

    editor.addInstance(textAreaId); 
    var editorInstance = editor.instanceById(textAreaId) 

    // Add the initial Placeholder 
    var content = editorInstance.getContent(); 
    if(content == "" || content == "<br>"){ 
     editor.instanceById(textAreaId).setContent($placeholder); 
    } 

    // onFocus - Remove the placeholder 
    editor.addEvent('focus', function() 
    { 
     if (editorInstance.getContent() == $placeholder){ 
      editor.instanceById(textAreaId).setContent("<br>"); 
     } 
    }); 

    // onBlur - Re-add the placeholder 
    editor.addEvent('blur', function() 
    { 
     var newText = editorInstance.getContent(); 
     if(newText == "" || newText == "<br>") { 
      editor.instanceById(textAreaId).setContent($placeholder); 
     } 
    }); 
}); 

最初,檢查,看看是否有任何內容,或者只有一個<br>(這是nicEditor的佔位符)。如果是這樣,請用您的佔位符替換。
然後重點檢查佔位符並將內容更改回"<br>"
然後模糊檢查內容是否仍爲空,並再次用佔位符替換它