简体   繁体   English

针对单击的元素执行功能

[英]Execute a function with respect to the clicked element

I am trying to execute a function with respect to the clicked element (here div with .redactor class). 我正在尝试执行有关clicked元素的功能(此处为.redactor类的div)。

If clicked the specified element ( .redactor ), then execute this function: 如果单击指定的元素( .redactor ),请执行以下功能:

$('.redactor').on("click", function() {
    $(".redactor").each(function () {
        if($(this).hasClass("selected"))
        {
             destroy_redactor(current_edit);
             $(this).removeClass("selected");
        }
    });

    $(this).addClass("selected");
    current_edit = $(this);
    initialize_redactor(current_edit);
});

Or if clicked other than the specified element, execute this function if there is already an initialized redactor or else do nothing: 或者,如果单击了除指定元素以外的其他元素,则如果已经有一个已初始化的编辑器,则执行此功能,否则不执行任何操作:

destroy_redactor(current_edit);

I saw this post as a reference. 我看到这篇文章作为参考。 But it is not helping. 但这没有帮助。 It just removes all the divs. 它只是删除所有的div。

I can't figure out how to execute the destroy_redactor(current_edit) function when clicked outside the .redactor div while there is already an initialized redactor for the current_edit . 当在.redactor div之外单击时,我已经不知道如何执行destroy_redactor(current_edit)函数,而current_edit已经有一个初始化的编辑器。

Sample in codepen.io codepen.io中的示例

how about adding the click listener on the document... 如何在文档上添加点击侦听器...

$(document).click(function(event) {
  var target = $(event.target);

  if (target.attr('class') == 'redactor') {
     //do something with it
  }
  else{
     // clicked on none of the redactor elements 
  }
});

if you need to find if there is a redactor active you can do something like this 如果您需要查找是否存在编辑器,可以执行以下操作

$(".redactor.active") //will get you a div with both redactor and active class

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

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