添加 tinymce 4 后,textarea 事件不起作用
textarea events are not working after adding tinymce 4
我在文本区域的 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>
这可能是因为 TinyMCE 替换了 onFocus 事件。尝试在 加载 tinyMCE 后 绑定 onFocus 事件。
tinyMCE.init({
//(...)
});
document.getElementById('txtDescription').addEventListener('focus', function() {
alert("Hello");
});
这里的问题是您的文本区域被 tinymce 隐藏了,只有在必要时 tinymce 才会使用它来将 tinymce 内容写回其中。
Tinymce 创建了自己的 iframe,使用户可以 insert/edit 格式化内容。您必须将焦点事件绑定到此 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.');
});
我在 Angular 10
中找到了解决方案
<!-- the text area declared in the components' html file-->
<textarea
class="form-control"
id="editor"
formControlName="contenu"
> </textarea>
// recovering the textarea by its id inside the components' typescript file
// important - after having initialised tinymce
this.textEditor = tinymce.get('editor');
// adding the event to the text area
this.textEditor.on('change', () => {
this.contenuModifie = true;
console.log('changed');
});
我在文本区域的 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>
这可能是因为 TinyMCE 替换了 onFocus 事件。尝试在 加载 tinyMCE 后 绑定 onFocus 事件。
tinyMCE.init({
//(...)
});
document.getElementById('txtDescription').addEventListener('focus', function() {
alert("Hello");
});
这里的问题是您的文本区域被 tinymce 隐藏了,只有在必要时 tinymce 才会使用它来将 tinymce 内容写回其中。
Tinymce 创建了自己的 iframe,使用户可以 insert/edit 格式化内容。您必须将焦点事件绑定到此 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.');
});
我在 Angular 10
中找到了解决方案<!-- the text area declared in the components' html file-->
<textarea
class="form-control"
id="editor"
formControlName="contenu"
> </textarea>
// recovering the textarea by its id inside the components' typescript file
// important - after having initialised tinymce
this.textEditor = tinymce.get('editor');
// adding the event to the text area
this.textEditor.on('change', () => {
this.contenuModifie = true;
console.log('changed');
});