简体   繁体   English

jQuery检查对象元素是否在另一个内部

[英]jQuery check if object element is inside another

I am trying to create a simple jQuery popup menu, but cant seem to figure out the script to check if an element is in a predefined element. 我正在尝试创建一个简单的jQuery弹出菜单,但似乎无法找出脚本来检查元素是否在预定义元素中。

Clicking on the popup trigger launches the jQuery function. 单击弹出窗口触发器启动jQuery函数。 The function saves the following variables: The element that was clicked (js_popup-trigger), its main parent (js_popup-container), and the actual menu with the menu items (js_popup-menu). 该函数保存以下变量:单击的元素(js_popup-trigger),其主要父级(js_popup-container)和带有菜单项的实际菜单(js_popup-menu)。

Once the trigger is clicked, the menu fades in/out, and an event listener is added to the document to check if the user clicked outside of the menu, in which case it should close, nothing should happen to the menu if the user clicks inside of it. 单击触发器后,菜单会淡入/淡出,并且会在文档中添加一个事件侦听器以检查用户是否在菜单外单击,在这种情况下它应该关闭,如果用户单击,菜单就不会发生任何事情在里面。

This is the part i cant figure out - how do i detect if the user has clicked inside the popup element or outside of it? 这是我无法弄清楚的部分 - 我如何检测用户是否已点击弹出元素内部或外部?

I have tried this: if(!$(e.target).parent('.js_popup-container').length){ 我试过这个: if(!$(e.target).parent('.js_popup-container').length){

But this does not work well when you have multiple popups on the page, because if you click anywhere else, including another popup, the current one should close, but since if you click on another popup with the same class of js_popup-container, then it will remain open because it technically is inside of an element with that class. 但是当你在页面上有多个弹出窗口时,这不能很好地工作,因为如果你点击其他任何地方,包括另一个弹出窗口,当前的一个应该关闭,但是如果你点击另一个弹出窗口具有相同类的js_popup-container,那么它将保持打开状态,因为它在技术上属于该类的元素。

/*  Popup menu toggle
===========================================*/
$(document).on("click",".js_popup-trigger", function(e){
    e.preventDefault();


    //Set variables
    var element     = $(e.currentTarget),
        container   = element.parent('.js_popup-container'),
        menu        = element.next('.js_popup-menu');


    //Toggle the menu
    menu.fadeToggle(200);


    /*  Create a click listener for the document
    *   This is to close the menu if clicked outside of it */
    $(document).click(function closePopup(e){


        //If the clicked item is not in the popup, close it
        if(!$(e.target).parent(container).length){
            menu.fadeToggle(200);
            $(document).unbind('click',closePopup);
        }
    });
});

In the end, what i want to achieve is a function that can be applied to any popup menu just by adding the 3 classes. 最后,我想要实现的是一个函数,只需添加3个类就可以应用于任何弹出菜单。 There should be the option to have multiple popup menus on the same page, and when clicking anywhere outside of the menu, it should close. 应该可以选择在同一页面上有多个弹出菜单,当单击菜单外的任何位置时,它应该关闭。

Of course i am open to suggestions on improving the code, but i don't want to use a preexisting plugins at this point. 当然我对改进代码的建议持开放态度,但我现在不想使用预先存在的插件。

EDIT: I know that there are already other questions and solutions very similar to this one, but the difference is that the solutions given in other questions is to check if the target that was clicked is inside of another element with a specific class name/id/selector. 编辑:我知道已经有其他问题和解决方案与此非常相似,但区别在于其他问题中给出的解决方案是检查被点击的目标是否在具有特定类名/ id的另一个元素内/选择器。

I need something a little bit different, i already have the parent element (the popup container) saved in a variable. 我需要一些不同的东西,我已经将父元素(弹出容器)保存在变量中。 I need to know if the clicked element is inside of that saved element. 我需要知道被点击的元素是否在该已保存元素内。

Using the solutions already found, i run in to a problem because i will have multiple popups on the page, so if i have one popup open, and i click on another, the first popup will remain open instead of closing, that's because using the other solutions, i am checking for an element with the popups container class, which they both have, which means that the first popup wont close. 使用已经找到的解决方案,我遇到了一个问题,因为我将在页面上有多个弹出窗口,所以如果我打开一个弹出窗口,然后我点击另一个弹出窗口,第一个弹出窗口将保持打开而不是关闭,这是因为使用其他解决方案,我正在检查弹出容器类的元素,他们都有,这意味着第一个弹出窗口不会关闭。

It depends on how performant you want your method to be, but here's one concise option assuming element and container are both jQuery objects. 这取决于你想要你的方法的性能,但这里有一个简洁的选择,假设元素和容器都是jQuery对象。

if(element.parents().find(container).length == 1){
   // do work
}

I came up with a solution that is working for my current app at the moment. 我想出了一个适用于我当前应用程序的解决方案。

$(document).on("click",".js_popup-trigger", function(e){
    e.preventDefault();

    //Set variables
    var element     = $(e.currentTarget).closest('.js_popup-trigger'),
        container   = element.parent('.js_popup-container'),
        menu        = element.next('.js_popup-menu');

    //Toggle the menu
    menu.fadeToggle(200);

    //Add the open classes to the container and trigger
    container.toggleClass('js_popup-container--open');
    element.toggleClass('js_popup-trigger--open');

    /*  Create a click listener for the document
    *   This is to close the menu if clicked outside of it */
    $(document).click(function closePopup(e){

        //If the clicked item is not in the popup, close it
        if(!$(e.target).closest(container[0]).length && element.is(":visible")){

            //Close the popup
            menu.fadeOut(200);

            //Remove the added classes
            container.removeClass('js_popup-container--open');
            element.removeClass('js_popup-trigger--open');

            //Unbind the closePopup function from the document
            $(document).unbind('click',closePopup);
        }
    });
});

This makes it so that you can add as many popups as you want to the same page, and they will all work independently of others - all other solutions i found used a general class to check if the popup is open instead of an actual element/object, which meant that if you had another popup open with the same class, then clicking on a new popup would not close the first one. 这使得你可以在同一页面上添加任意数量的弹出窗口,并且它们将独立于其他人工作 - 我发现的所有其他解决方案都使用通用类来检查弹出窗口是否打开而不是实际元素/对象,这意味着如果你有另一个弹出窗口打开同一个类,那么单击一个新的弹出窗口将不会关闭第一个弹出窗口。 This approach fixes that problem. 这种方法解决了这个问题。

Hope this is helpful to others, if anyone has better ideas then i would love to hear them! 希望这对其他人有帮助,如果有人有更好的想法,那么我很乐意听到他们!

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

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