简体   繁体   English

ckeditor中插入的html按钮的onclick事件

[英]Onclick event of inserted html button in ckeditor

I'm writing a plugin for ckeditor that will add s to the content, but I have run into a problem with the onclick , as it conflicts with the functionality of ckeditor.我正在为 ckeditor 编写一个插件,该插件会将 s 添加到内容中,但是我遇到了onclick问题,因为它与 ckeditor 的功能冲突。

If I register an onclick using:如果我使用以下方法注册onclick

commit: function( element ) {
    element.setAttribute( 'onclick' , 'window.location=\'' + this.getValue() + '\'');
}

In my dialog, clicks on the button will not open the edit element dialog of my plugin, but rather send me to the target of the buttons onclick .在我的对话框中,单击按钮不会打开插件的编辑元素对话框,而是将我发送到按钮的目标onclick

Is there any way around this?有没有办法解决?

After a lot of fiddling around I concluded that it was not possible to use custom onclicks i ckeditor the way I wanted.经过大量摆弄之后,我得出结论,不可能按照我想要的方式使用自定义的 onclicks ickeditor。 Instead i devised the following workaround, since I am really only interested in the data returned from editor.getData() to be correct:相反,我设计了以下解决方法,因为我真的只对从 editor.getData() 返回的数据是否正确感兴趣:

Instead of registering onclick like this in the dialog而不是在对话框中像这样注册 onclick

element.setAttribute( 'onclick' , 'window.location=\'' + this.getValue() + '\'');

I created a custom data attribute for the location.href我为 location.href 创建了一个自定义数据属性

element.setAttribute( 'data-custom-click' , 'location.href=\'' + this.getValue() + '\'');

Then, in the init of the plugin that creates the buttons and registers the above mentioned dialog, I registered two event handlers然后,在创建按钮和注册上述对话框的插件的 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, according to the documentation根据文档, getData触发

before the getData call returns, allowing for additional manipulation.在 getData 调用返回之前,允许进行额外的操作。

setData , on the other hand, fires另一方面, setData会触发

before the setData call is executed, allowing for additional manipulation.在执行 setData 调用之前,允许进行额外的操作。

So any editor with these event handlers will transform onclick to data-custom-click when loading html, thus rendering my buttons safe while editing, and transform any data-custom-click back to onclick when editor.getData() is called, giving me working buttons for the "real" page.因此,任何具有这些事件处理程序的编辑器都会在加载 html 时将 onclick 转换为 data-custom-click,从而在编辑时使我的按钮安全,并在调用 editor.getData() 时将任何 data-custom-click 转换回 onclick,给我“真实”页面的工作按钮。

It is kind of a hack, but it works =)这有点像黑客,但它有效 =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM