CKEditor - get element after click on context Menu

社会主义新天地 提交于 2019-12-23 08:55:08

问题


I added link to context menu of img in ckeditor, using this code CKEditor - Add Context Menu Item to Images

How can I get the information about the image, on which user clicked? For example the id of the image. Or the path. In order to process with the selected image.


回答1:


The solution was pretty easy.

$('body').on('contextmenu','img',function(){
var imgid = $(this).attr('id');
alert(imgid);
})

Using jquery to track click on image, we can save it's id to global variable. Then, inside the command of the plugin, to take the id that we saved before.




回答2:


In JavaScript this keyword refers to the owner of a function or event. So when you write click event handler for elements on HTML document. Then this will return particular html element where click event executed. So inside you click event handler function, use this.

this keyword has properties depending upon element but id and name are common for most of html elements. For e.g. here in img element, src property could return url attribute value of image.

This is good source to know more about this keyword http://www.quirksmode.org/js/this.html




回答3:


You can use the function of editor getSelection() to know the element clicked for the context menu :

exec: function (editor) {
     var selection = editor.getSelection();
     var selectedElement = selection.getStartElement();

     // Use it as jquery object to get id or more ...
     $(selectedElement.$);
}


来源:https://stackoverflow.com/questions/42012440/ckeditor-get-element-after-click-on-context-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!