我在textarea的onfocus事件上调用一个函数,它工作正常.但是在加入tinymce之后它停止了工作.
这是我的代码
JavaScript的…
<script src="tinymce/tinymce.min.js" type="text/javascript"></script>
<script type="text/javascript">
tinyMCE.init({
theme: "modern",
mode: "exact",
elements: "txtDescription",
auto_focus: false,
resize: false,
convert_urls: false,
menubar: false,
paste_data_images: true,
plugins: ['advlist autolink lists link image charmap print preview hr anchor pagebreak searchreplace visualblocks visualchars media nonbreaking table contextmenu template paste textcolor'],
toolbar: "bold italic underline charmap subscript superscript table link image media, formulabutton, imagebutton",
height: 150,
width: 600
});
function ShowMessage() {
alert("Hello");
}
</script>
和HTML …
<div>
<textarea id="txtDescription" onfocus="ShowMessage()"></textarea>
</div>
解决方法:
这里的问题是你的textarea被tinymce隐藏起来,只有在必要时才会被tinymce用来将tinymce内容写回来.
Tinymce创建了一个自己的iframe,使用户可以插入/编辑格式化的内容.您必须将焦点事件绑定到此iframe或更好的iframe主体.这是代码:
var my_editor = tinymce.get('my_editor_id'); // or tinymce.editors[0]
$(my_editor.getBody()).bind('focus', function(e){
console.log('Tinymce body iframe has been focused.');
});
要么
var my_editor = tinymce.get('my_editor_id'); // or tinymce.editors[0]
my_editor.getBody().addEventListener('focus', function() {
console.log('Tinymce body iframe has been focused.');
});