0
我正在编辑一个小编辑器,我希望能够右键单击图像,然后对其执行操作。通过自定义jQuery下拉菜单将类应用于img
我有自定义右键单击菜单工作,但我不知道如何告诉jQuery哪个项目我右键单击。
的jsfiddle - >http://jsfiddle.net/kthornbloom/YsW8D/1/
代码:
// Make the menu
$('.something img').bind("contextmenu", function(event) {
event.preventDefault();
$("div.custom-menu").hide();
$('<div class="custom-menu"><a href="#" class="fr">Float Right</a></div>')
.appendTo("body")
.css({top: event.pageY + "px", left: event.pageX + "px"});
});
// Remove the menu
$(document).bind("click", function(event) {
$("div.custom-menu").hide();
event.preventDefault();
});
// Apply option to image you right-clicked on
$(document.body).on("click", ".run", function (event) {
// this won't work, but is what I want to happen.
$(this).nearest('img').addClass('float-right');
event.preventDefault();
});
谢谢你的解释。有没有办法在第二个函数中使用“this”,并且意味着它与原始函数中的一样?点击下拉列表中的一个选项后,请参阅第25行,我试图将类应用于图像。 – kthornbloom
@kthornbloom什么是“.run” –
对不起,忘了更新。新的jsfiddle:http://jsfiddle.net/kthornbloom/YsW8D/2/我最终会在下拉菜单中有几个选项。所以当你点击其中一个选项时,它应该对你右键点击的图像做一些事情。 – kthornbloom