为什么 jQuery UI 的日期选择器会与动态 DOM 中断? [英] Why does jQuery UI's datepicker break with a dynamic DOM?

查看:13
本文介绍了为什么 jQuery UI 的日期选择器会与动态 DOM 中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里使用了一个动态 DOM,并且已经将 jQuery UI 日期选择器调用到具有特定类名的所有输入,在本例中为 .date

I'm working with a dynamic DOM here, and have called the jQuery UI datepicker to all inputs with a specific class name, in this case .date

它适用于第一个静态构造,但是当我克隆它时,事件处理程序似乎不想移动.我收到 Firebug 错误:

It works great with the first, static, construct but when I clone it the event handlers don't seem to want to move over. I get the Firebug error:

inst 未定义

我尝试查看 jQuery 的新 live() 函数,但无法将两者结合起来.有什么想法吗?

I tried looking into jQuery's new live() function but couldn't combine the two. Any ideas?

推荐答案

啊,明白了.在我将 HTML 附加到 DOM 之后,我在所有想要弹出日期选择器的输入上运行它.Datepicker 将一个类添加到它已附加到的元素,因此我们可以过滤掉现有的输入并仅将其应用于新的输入.

Ah, got it. Right after I append the HTML to the DOM I run this on all the inputs I'd like to have a datepicker pop up with. Datepicker adds a class to elements it has been attached to, so we can filter out existing inputs and only apply it to new ones.

$('.date').not('.hasDatePicker').datepicker();

我希望这对人们有所帮助,因为我在谷歌上搜索了好几天并没有找到任何东西!

I hope this helps people as I was Googling for days and didn't find anything!

您还应该注意,通过将其设置为上下文而不是整个页面,在新生成的 HTML 中检查 input.date 会更快,因为它会保存时间,因为这是一个更有效的操作.

You should also note that it would be faster to check for input.date in the new generated HTML by setting that as a context, rather than the whole page, as it will save time, due to this being a more efficient operation.

这篇关于为什么 jQuery UI 的日期选择器会与动态 DOM 中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆