我想在我的页面中调用一个名为enableTinyMCE()的函数调用;在该函数中,我想看看我的页面中是否有任何textarea,如果是,还是tinyMCE.init()函数。如何检测页面中是否有任何textarea元素?
答案 0 :(得分:8)
if( $('textarea').length > 0 ) {
...
}
答案 1 :(得分:7)
if($('textarea').length > 0) {
document.write('we have at least one textarea');
}
OR
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
答案 2 :(得分:5)
值得注意的是,您正在寻找的功能可由TinyMCE本身处理:
如果在tinyMCE.init()调用中将mode
参数设置为textareas
,它会自动将找到的任何textareas转换为编辑器实例。如果没有textareas,它将不会做任何事情,悄悄地。
tinyMCE.init({
...
mode : "textareas",
...
});
相反,您可以告诉TinyMCE只使用specific_textareas
值转换为mode
参数来转换与CSS类名匹配的textareas。
tinyMCE.init({
...
mode : "specific_textareas",
editor_selector : "mceEditor"
});
http://wiki.moxiecode.com/index.php/TinyMCE:Configuration/mode
答案 3 :(得分:4)
扩展karim79的答案。
来自jQuery docs page:注意:并不总是需要测试元素是否存在。以下代码将显示该项目是否存在,如果不存在则不执行任何操作(无错误)。
这意味着您可以这样做:
$(function(){
$("textarea").each(function(i){
this.enableTinyMCE();
})
})
修改强>
实际上有一个jQuery插件正在为此目的而开发。如果可以,我会下载并试用该插件并为其开发做出贡献。
答案 4 :(得分:2)
即使找不到匹配的元素,jQuery选择器也总是返回一个数组。这意味着您需要检查长度。试试这个。
if($('textarea').length > 0) {
document.write('we have at least one textarea');
}
答案 5 :(得分:1)
我按照建议完成了以下操作,但是如果我的页面中有textareas,我仍然只想调用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'
});
}
需要在documentready之外调用tinymce.init,但是$('textarea')。在documentready之外,length总是为零。救命啊!
答案 6 :(得分: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'
});
}