简体   繁体   English

CKEditor - 单击上下文菜单后获取元素

[英]CKEditor - get element after click on context Menu

I added link to context menu of img in ckeditor, using this code CKEditor - Add Context Menu Item to Images 我在ckeditor中添加了img上下文菜单的链接,使用此代码CKEditor - 将上下文菜单项添加到图像

How can I get the information about the image, on which user clicked? 如何获取有关用户单击的图像的信息? For example the id of the image. 例如图像的id。 Or the path. 或者路径。 In order to process with the selected image. 为了处理选定的图像。

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. 使用jquery跟踪点击图像,我们可以将它的id保存到全局变量。 Then, inside the command of the plugin, to take the id that we saved before. 然后,在插件的命令内,获取我们之前保存的id。

In JavaScript this keyword refers to the owner of a function or event. 在JavaScript中,此关键字指的是函数或事件的所有者。 So when you write click event handler for elements on HTML document. 因此,当您为HTML文档上的元素编写click事件处理程序时。 Then this will return particular html element where click event executed. 然后,这将返回执行click事件的特定html元素。 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. 此关键字具有取决于元素的属性,但id和name对于大多数html元素是常见的。 For eg here in img element, src property could return url attribute value of image. 例如,在img元素中,sr​​c属性可以返回image的url属性值。

This is good source to know more about this keyword http://www.quirksmode.org/js/this.html 这是了解有关此关键字http://www.quirksmode.org/js/this.html的更多信息的良好来源

You can use the function of editor getSelection() to know the element clicked for the context menu : 您可以使用编辑器getSelection()的功能来了解为上下文菜单单击的元素:

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

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

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

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