jquery live() 和插件
我正在使用 jquery 插件,统一,这样在我的表单上我可以有漂亮的单选按钮和文件输入等。
但是我遇到了一个问题,我的很多表单都是通过 ajax 拉入的,并且显然插件需要工作知道元素存在于 dom 中。
有没有办法通过ajax方法使用插件?
目前我正在尝试
$(".uniform input").live().uniform();
这显然不起作用,有什么建议吗?
---在加载回调中实现-----
$('#upload').click(function(e) {
$("#content #main").animate({"right": "-900px"}, 'slow', 'linear', function(){
$(this).load('upload.html #main div', '', function(){
$(".uploader input").uniform();
});
});
$("#main").animate({"right": "0px"}, 'slow', 'swing');
e.preventDefault();
});
I am using the jquery plugin, uniform so that on my form I can have pretty radio buttons, and file inputs etc.
However I have hit a problem alot of my forms a pulled in via ajax, and obvioulsy for the plugin to work it needs to know that the elements exists in the dom.
Is there away to use plugins with ajax methods?
Currently I am trying,
$(".uniform input").live().uniform();
which obviously does not work, any suggestions?
---IMPLENTED IN LOAD CALLBACK-----
$('#upload').click(function(e) {
$("#content #main").animate({"right": "-900px"}, 'slow', 'linear', function(){
$(this).load('upload.html #main div', '', function(){
$(".uploader input").uniform();
});
});
$("#main").animate({"right": "0px"}, 'slow', 'swing');
e.preventDefault();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 livequery 插件,如下所示:
You could use the livequery plugin, like this:
.live 和 .delegate 之间的区别之一是可链接性。请查看 http://brandonaaron.net/blog/2010/03 /4/event-delegation-with-jquery 对此进行了很好的评论。这看起来您可能会在您的情况下使用委托。
One of the differnces between .live and .delegate is chainability. Please review http://brandonaaron.net/blog/2010/03/4/event-delegation-with-jquery for a nice review on this. This looks likely that you would use delegate in your situation.
回应评论:
.load
只不过是包装在另一个方法中的$.ajax
调用。它也有回调。编辑:我刚刚看到你的编辑。如果这不起作用,请确保您使用正确的选择器来访问要应用插件的元素。
In response to comments:
.load
is nothing more than a$.ajax
call wrapped in another method. It too has a callback.Edit: i just saw your edit. If that isn't working, make sure you are using the correct selector to get to the elements you want to apply the plugin to.
只需在表单中添加之后再次初始化统一,使用正常
Just initialize uniform again after you've added in a form, using the normal