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

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

问题描述

我在这里使用动态DOM,并已将jQuery UI datepicker调用到具有特定类名的所有输入,在本例中为.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未定义

inst is undefined

我尝试研究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天全站免登陆