【问题标题】:get next or live element with jQuery find使用 jQuery find 获取下一个或实时元素
【发布时间】:2012-01-26 12:04:33
【问题描述】:

好的,不确定find() 是否是正确的方法。我正在使用pretty photo,并且正在加载一些内联内容。基本上,当漂亮的照片启动时,它会复制内联定义的内容并将其显示在灯箱中。

这里的问题是我不能调用$("#myTextField").datepicker(); 来激活文本框上的jQuery UI Datepicker。原因是它将它应用于我最初编写的不可见的,而不是可见的克隆的。我之前使用内联内容中的Submit 按钮遇到了这个问题,我使用$("#mySubmitButton").live('click',function()..... 解决了这个问题

我试过$(this).find("#myTextField").next().datepicker();,但没有骰子。还有其他建议吗?我不知道我还能如何使用live() 方法。

【问题讨论】:

  • 我设法让日期选择器弹出,除了当我点击一个数字时数据选择器没有插入日期。我假设它把它放在原来的隐形内容$("#shoot_date").live("focus",function(){$(this).datepicker();});
  • Check-out .delegate(), .live() 自 jQuery 1.7 起已贬值:api.jquery.com/delegate

标签: jquery-ui find jquery next


【解决方案1】:

好的,我通过:Jquery datepicker does not work inside lightbox找到了它

$("#shoot_date").live("focus",function()
{
    $(this).attr("id","datepickerNEWID");                 
    $(this).datepicker();
});

【讨论】:

    【解决方案2】:

    这是因为 datepicker 的事件没有附加到任何克隆的元素上。

    您可以使用 prettyPhoto 方法 changepicturecallback,将 datepicker 绑定到克隆元素上。 (基于documentation

    $("a[rel^='prettyPhoto']").prettyPhoto({
        changepicturecallback: function(){/* Called everytime an item is shown/changed */
            $("#myTextField").unbind("datepicker"); // this will unbind previous binded event
            $("#myTextField").datepicker(); // bind new event
        }
    });
    

    或者,尝试在行内元素中添加$("#myTextField").datepicker();。当然,在脚本标签中。当 prettyPhoto 启动时,它将绑定该事件。

    【讨论】:

    • 虽然在 prettyphoto 中确实调用了回调,但仍未创建 unbind 和 datepicker 方法。我认为他们仍然引用原始内容而不是克隆内容
    • 你使用 ID 作为选择器还是类?试试 $('.pp_content_container .date_class').unbind("datepicker").datepicker();
    • 好的,但是日历上的所有事件都不起作用。当我尝试选择日期时,我收到错误 Uncaught TypeError: Cannot set property 'currentDay' of undefined。我想我需要走一条不同的路才能让它发挥作用
    • 如果我这样做,我会得到相同的结果:$("#shoot_date").live("focus",function(){$(this).datepicker();});。同样的错误是
    【解决方案3】:

    如果您知道“Pretty Photo”的 HTML 结构(您可以使用 FireBug 或任何其他开发人员工具找到它),那么您可以创建一个选择器,在“Pretty Photo”元素中找到克隆的元素:

    $('#pretty-photo-id').find('input[type="text"]').datepicker();
    

    这很可能没有针对您的情况进行优化,但根据我所掌握的信息,这是我所能获得的最具体的信息。

    【讨论】:

      【解决方案4】:

      不要使用“#myTextField”之类的 id 选择器,而应考虑使用“.dateField”之类的类选择器。

      在同一页面上有多个具有相同 id 的元素可能是您的麻烦之源。

      【讨论】:

        猜你喜欢
        • 2011-01-11
        • 2011-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-03
        • 1970-01-01
        相关资源
        最近更新 更多