简体   繁体   English

JQuery UI datepicker next 和 prev 按钮只有一个祖先。 $(e.target).parents() 只返回一个元素

[英]JQuery UI datepicker next and prev buttons only have one ancestor. $(e.target).parents() only returns one element

I don't have enough rep to post my own answer just yet, but here it is before I waste more peoples' time:我还没有足够的代表来发布我自己的答案,但在我浪费更多人的时间之前:

Ok, I now understand why I don't get all of the expected ancestors:好的,我现在明白为什么我没有得到所有预期的祖先:

JQuery datepicker deletes the parent node (the datepicker-head) onclick. JQuery datepicker 在点击时删除父节点(datepicker-head)。 After that my event is triggered on the button of the element that already got deleted.之后我的事件在已经被删除的元素的按钮上触发。 So now I am "trapped" in the scope of the deleted element and can only traverse up to the deleted container itself.所以现在我“被困”在被删除元素的范围内,只能遍历到被删除的容器本身。 I think I can work out a solution for my event-delegation with this knowledge!我想我可以用这些知识为我的事件委托制定一个解决方案!

I will add this as the proper answer to my problem in 8 hours!我将在 8 小时内将此添加为我的问题的正确答案! ;) ;)

The original question/problem: I've already thoroughly searched stackoverflow, google etc. for this problem and can't seem to find a solution.最初的问题/问题:我已经针对这个问题彻底搜索了 stackoverflow、google 等,但似乎找不到解决方案。

I am going crazy over this problem: I have a JQuery UI Datepicker in a container that hides if you click outside of it.我对这个问题快疯了:我在一个容器中有一个 JQuery UI Datepicker,如果你在它外面点击它就会隐藏。 That means I need to know when there is interaction inside the container so it does not get closed!这意味着我需要知道容器内何时发生交互,以便它不会被关闭! On document.click I am traversing up from the given element to find my enclosing container (or meet any other condition) to either allow the action, or deny it.在 document.click 上,我从给定元素向上遍历以找到我的封闭容器(或满足任何其他条件)以允许操作或拒绝操作。 This works like a charm - also clicks onto the generated datepicker work, except for the 'next month' and 'previous month' buttons.这就像一个魅力 - 也可以点击生成的日期选择器工作,除了“下个月”和“上个月”按钮。 In the following fiddle I am logging the .parents() of the clicked element.在下面的小提琴中,我记录了被点击元素的 .parents() 。 Note that if you click the next and prev buttons the document.click will go through, closing the container and the log will only show the immediate parent of the buttons.请注意,如果您单击 next 和 prev 按钮,document.click 将通过,关闭容器,日志将仅显示按钮的直接父级。 If you click other elements in the datepicker you will see that the .parents() will give you all elements up to the enclosing html-tag.如果您单击日期选择器中的其他元素,您将看到 .parents() 将为您提供所有元素,直到封闭的 html 标签。

Does anyone have any idea of how to fix this?有谁知道如何解决这个问题?

I made a simple example to showcase this... Here is the html:我做了一个简单的例子来展示这一点......这是html:

<div class="container">
    <input type="text" class="myToggleInput" />
    <div class="toggleContainer" style="display: none">
        <div class="myDatepicker"></div>
    </div>
</div>

This is the script:这是脚本:

$(document).ready(
    init()
);

function init() {
    $('.myDatepicker').datepicker();
    $(document).on('click', '.myToggleInput', function(e) {
        $('.toggleContainer').show();
    });

    $(document).on('click', function(e) {
        //Logging the targets parents
        //Note that the next and prev buttons only go up one level in the DOM Tree, thus
        //not returning all of their parents...
        console.log($(e.target).parents());
        if ($(e.target).parents('.toggleContainer').length == 0 && !$(e.target).hasClass('myToggleInput')) {
            $('.toggleContainer').hide();
        }
    });
};

Here is the fiddle: http://jsfiddle.net/Px9Ab/这是小提琴: http : //jsfiddle.net/Px9Ab/

To clarify: Yes, I could add the classes of the next/prev buttons to my conditions, but I'd rather like a general solution, so I can be sure that '$(e.target).parents()' always traverses up to the html-node.澄清:是的,我可以将下一个/上一个按钮的类添加到我的条件中,但我更喜欢通用解决方案,因此我可以确定 '$(e.target).parents()' 总是遍历直到 html 节点。

Why I can't use stoppropagation: There is a lot of dynamic stuff going on inside this container and the rest of the website.为什么我不能使用 stoppropagation:这个容器和网站的其余部分有很多动态的东西。 So for example I am reloading a list of elements dependent on the chosen date, which also have events attached to them, or rather delegate up to document as well.因此,例如,我正在重新加载依赖于所选日期的元素列表,这些元素也有附加到它们的事件,或者更确切地说是委托给文档。 That's why I can't stop propagation on the container.这就是我无法停止在容器上传播的原因。

Any other solutions that completely circumvent my issue are welcome too!也欢迎任何其他完全绕过我的问题的解决方案! :) :)

JQuery datepicker deletes the parent node (the datepicker-head) onclick. JQuery datepicker 在点击时删除父节点(datepicker-head)。 After that my event is triggered on the button of the element that already got deleted.之后我的事件在已经被删除的元素的按钮上触发。 So now I am "trapped" in the scope of the deleted element and can only traverse up to the deleted container itself.所以现在我“被困”在被删除元素的范围内,只能遍历到被删除的容器本身。

To circumvent my original problem I had to filter out all the clickable elements that do not bubble up.为了规避我最初的问题,我必须过滤掉所有不会冒泡的可点击元素。

Eg:例如:

$(document).on('click', function(e) {
    if (!$(e.target).hasClass('ui-dialog')) {
        window.XYZ.hideAllDatePickersPseudoMethod();
    }
});

I know that's more some kind of a workaround than a fix for the problem, but it's the only solution I came up with that suits all my needs.我知道这更像是一种解决方法而不是解决问题,但这是我想出的唯一满足我所有需求的解决方案。

Just stop event propagation for any click events triggered on the .toggleContainer element:只需停止在.toggleContainer元素上触发的任何click事件的事件传播:

$('.toggleContainer').on('click', function(e) {
   e.stopPropagation(); 
});

$(document).on('click', function(e) {
    $('.toggleContainer').hide();
});

jsFiddle demo jsFiddle 演示

Use event.stopPropagation on input and .toggleContainer and datepicker navigational:在输入和 .toggleContainer 和 datepicker 导航上使用 event.stopPropagation:

$('.myDatepicker').datepicker();

$(document).on("click", '.myToggleInput', function () {
    $('.toggleContainer').show();
});
$(document).on("click", function () {
   $('.toggleContainer').hide();
});
$(document).on("click", ".toggleContainer, .myToggleInput, .ui-datepicker-next, .ui-datepicker-prev", function (e) {
    e.stopPropagation();
});

http://jsfiddle.net/Px9Ab/5/ http://jsfiddle.net/Px9Ab/5/

$('.myDatepicker').datepicker();

$(document).on("click", '.myToggleInput', function () {
    $('.toggleContainer').toggle();
});
$(document).on("click", function () {
   $('.toggleContainer').toggle();
});
$(document).on("click", ".toggleContainer, .myToggleInput, .ui-datepicker-next, .ui-   datepicker-prev", function (e) {
    e.stopPropagation();
});

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

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