6

我想在我的页面中调用 enableTinyMCE(); 在该函数中,我想查看我的页面中是否有任何 textarea,如果有,则使用 tinyMCE.init() 函数。如何检测页面中是否有任何 textarea 元素?

4

7 回答 7

8
if( $('textarea').length > 0 ) {
    ...
}
于 2009-06-04T13:29:09.823 回答
7
if($('textarea').length > 0) {
    document.write('we have at least one textarea');
}

或者

if($('textarea').length) {
    document.write('we have at least one textarea');
}

从常见问题解答中阅读此内容:

http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_test_whether_an_element_exists.3F

于 2009-06-04T13:25:34.433 回答
5

值得注意的是,您正在寻找的功能可以由 TinyMCE 自己处理:

如果您在 tinyMCE.init() 调用中将mode参数设置为textareas,它会自动将找到的任何文本区域转换为编辑器实例。如果没有 textareas,它不会做任何事情,安静地。

tinyMCE.init({
    ...
    mode : "textareas",
    ...
});

相反,你可以告诉 TinyMCE 只转换与 CSS 类名匹配的文本区域,方法是使用参数的specific_textareasmode

tinyMCE.init({
   ...
   mode : "specific_textareas",
   editor_selector : "mceEditor"
});

http://wiki.moxiecode.com/index.php/TinyMCE:Configuration/mode

于 2009-06-27T15:59:53.977 回答
4

扩展 karim79 答案。

来自jQuery 文档页面:注意:并不总是需要测试元素是否存在。以下代码将显示该项目是否存在,如果不存在则不执行任何操作(无错误)。

这意味着您可以这样做:

$(function(){
    $("textarea").each(function(i){
        this.enableTinyMCE();
    })
})

编辑:

实际上,为此目的正在开发一个 jQuery 插件。如果可以的话,我会下载并试用该插件并为它的开发做出贡献。

jq-tinyMCE

于 2009-06-04T14:00:00.543 回答
2

即使没有找到匹配的元素,jQuery 选择器也总是返回一个数组。这意味着您需要检查长度。试试这个。

if($('textarea').length > 0) {    
 document.write('we have at least one textarea');
}
于 2009-06-04T13:29:08.997 回答
1

我按照建议完成了以下操作,但是如果我的页面中有文本区域,我仍然只想调用 enableTinyMCE。我无法在 document.ready 中调用此函数。见谷歌!有任何想法吗?

$(function() {

     if ($('textarea').length > 0)
    {
       var data = $('textarea');
       $.each(data, function(i)
       {
         tinyMCE.execCommand('mceAddControl', false, data[i].id);
       }
       );
    }

});

function enableTinyMCE()
{
        tinyMCE.init({
            plugins: 'paste',
            theme_advanced_toolbar_location: 'top',
            theme_advanced_buttons1: 'pastetext,pasteword,copy,cut,separator,bold,italic,underline,separator,bullist,numlist,separator,undo,redo,separator,link,unlink,separator,charmap,separator,formatselect,separator,code',
            theme_advanced_buttons2: '',
            theme_advanced_buttons3: '',
            mode: 'textareas',
            theme: 'advanced',
            theme_advanced_blockformats: 'None=p,Heading 3=h3,Heading 2=h2'
        });
}

tinymce.init 需要在 documentready 之外调用,但 $('textarea').length 在 documentready 之外始终为零。帮助!

于 2009-06-04T13:53:18.450 回答
0

这是我的解决方案

if ($('textarea').length > 0)
    {
       var data = $('textarea');
       $.each(data, function(i)
       {
         tinyMCE.execCommand('mceAddControl', false, data[i].id);
       }
       );

       $('form').bind('form-pre-serialize', function(e) {
            tinyMCE.triggerSave(true,true);
        });
    }

为了启用 tinyMCE,我已经这样做了

<% if (ViewData["TextAreaVisible"] != null && bool.Parse(ViewData["TextAreaVisible"].ToString()) == true)
   {%>
        <script type="text/javascript" src="../../Scripts/tinymce/tiny_mce.js"></script>
        <script type="text/javascript">
           enableTinyMCE();
        </script>
<%} %>

EnableTinyMCE 执行此操作

function enableTinyMCE() {

    tinyMCE.init({
        plugins: 'paste',
        theme_advanced_toolbar_location: 'top',
        theme_advanced_buttons1: 'pastetext,pasteword,copy,cut,separator,bold,italic,underline,separator,bullist,numlist,separator,undo,redo,separator,link,unlink,separator,charmap,separator,formatselect,separator,code',
        theme_advanced_buttons2: '',
        theme_advanced_buttons3: '',
        mode: 'none',
        theme: 'advanced',
        theme_advanced_blockformats: 'None=p,Heading 3=h3,Heading 2=h2'
    });

}

于 2009-06-05T14:00:50.473 回答