Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ckeditor中插入的html按钮的Onclick事件_Javascript_Ckeditor_Dom Events - Fatal编程技术网

Javascript ckeditor中插入的html按钮的Onclick事件

Javascript ckeditor中插入的html按钮的Onclick事件,javascript,ckeditor,dom-events,Javascript,Ckeditor,Dom Events,我正在为ckeditor编写一个插件,该插件将向内容中添加s,但我遇到了一个问题,即onclick,因为它与ckeditor的功能冲突 如果我使用以下方法注册onclick: commit: function( element ) { element.setAttribute( 'onclick' , 'window.location=\'' + this.getValue() + '\''); } 在我的对话框中,单击按钮不会打开插件的编辑元素对话框,而是将我发送到按钮的目标oncl

我正在为ckeditor编写一个插件,该插件将向内容中添加s,但我遇到了一个问题,即
onclick
,因为它与ckeditor的功能冲突

如果我使用以下方法注册
onclick

commit: function( element ) {
    element.setAttribute( 'onclick' , 'window.location=\'' + this.getValue() + '\'');
}
在我的对话框中,单击按钮不会打开插件的编辑元素对话框,而是将我发送到按钮的目标
onclick


有什么办法可以解决这个问题吗?

经过一番努力,我得出结论,不可能按照我想要的方式使用自定义onclick。相反,我设计了以下解决方法,因为我真正感兴趣的是从editor.getData()返回的数据是否正确:

而不是像这样在对话框中注册onclick

element.setAttribute( 'onclick' , 'window.location=\'' + this.getValue() + '\'');
我为location.href创建了一个自定义数据属性

element.setAttribute( 'data-custom-click' , 'location.href=\'' + this.getValue() + '\'');
然后,在创建按钮并注册上述对话框的插件的init中,我注册了两个事件处理程序

editor.on('getData', function(e) {
    e.data.dataValue = e.data.dataValue.replace(' data-custom-click=', ' onclick=');
});

editor.on('setData', function(e) {
    e.data.dataValue = e.data.dataValue.replace(' onclick=',' data-custom-click=');
});
getData
fires,根据文档

在getData调用返回之前,允许进行额外的操作

另一方面,
setData
,则会触发

在执行setData调用之前,允许进行额外的操作

因此,任何具有这些事件处理程序的编辑器在加载html时都会将onclick转换为数据自定义单击,从而使我的按钮在编辑时安全,并在调用editor.getData()时将任何数据自定义单击转换回onclick,为“真实”页面提供工作按钮


这是一种黑客行为,但它有效=)

您尝试过使用element.addEventListener吗?唉,@luis periera,parameter元素指的是CKEditors元素[,因此addEventListener不是一个函数。它有一个.on('event type',function),但它的行为与我最初的onclick解决方案相同