jQuery 中复制元素的绑定问题
这实际上是一个更大的问题,因为我知道有多种方法可以解决这个问题,但我会尝试总结一下。
我尝试做的事情:我正在使用这个 jQuery 插件通过 Flash http://www.uploadify.com/ 上传文件。但是,我应该将此函数绑定到的元素 #fileInput 是一个活动元素,它是在页面加载后生成的:$('#fileInput').uploadify()。 #fileInput 是一个活动元素的原因是因为我使用 FancyBox 弹出一个 DIV,而这个 FancyBox 基本上只是“克隆”了 DIV 的内部 html。
发生了什么:当我点击“浏览”上传文件时,没有上传进度条。原因是 Uploadify 无法绑定到活动元素。
问题: 1.我尝试在uploadify代码中将bind()替换为live(),但这不起作用,因为bind()允许传递[数据]。 LiveQuery 插件 http://docs.jquery.com/Plugins/livequery 没有与bind() 的语法相同。有没有类似于绑定但适用于活动元素的东西?
- 如果我不尝试替换bind()函数并保持uploadify代码相同。有谁知道如何更改 FancyBox 中的代码,以便它不会进行克隆来生成活动元素?我知道这也是一个很难的问题。
注意:FancyBox 网站似乎已经死了 --> http://www.visual-blast.com/javascript /fancybox-jquery-image-zooming-plugin/
非常感谢!
This is actually a bigger question because I know there are several ways to solve this problem but I will try to sum it up.
What I try to do: I am using this jQuery plugin to upload files via Flash http://www.uploadify.com/. However, the element #fileInput that I supposed to bind this function to is a live element which is generated after the page loaded: $('#fileInput').uploadify(). The reason #fileInput is a live element is because I use FancyBox to popup a DIV and this FancyBox basically just "cloned" the inner html of the DIV.
What happened: When I clicked "BROWSE" to upload a file, there is no progress bar for upload. The reason is because the Uploadify could not bind to live elements.
Questions:
1. I tried to replace bind() with live() in uploadify code but that did not work because bind() allows to pass [data]. The LiveQuery plugin http://docs.jquery.com/Plugins/livequery does not have the same syntax as bind() either. Is there anything similar to bind but works for live elements?
- If I don't try to replace bind() function and keep uploadify code the same. Does anyone know how to change code in FancyBox so that it WILL NOT make a clone to generate live elements? I know this is a hard question too.
Note: FancyBox site seems dead --> http://www.visual-blast.com/javascript/fancybox-jquery-image-zooming-plugin/
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以考虑更改 FancyBox 代码以支持在克隆 HTML 后调用回调函数。然后,将 uploadify() 调用放入回调函数中。
You might consider changing the FancyBox code to support calling a callback function after it clones the HTML. Then, put the uploadify() call in the callback function.
您可以重载
live
方法,使其支持data
作为第二个参数:替换
data
的所有实例>bind(...)
与live(...)
现在应该可以工作了。注意:您必须将重载方法放在其他所有方法之上。
You could overload the
live
method, making it supportdata
as the second parameter:Replacing all instances of
bind(...)
withlive(...)
should now work.Note: you'll have to put the overloaded method above everything else.
根据我的经验,我发现做到这一点的唯一方法是使用 livequery
它具有类似的语法,在您的情况下要在实时元素上绑定 uploadify,您将使用
Livequery 接受没有事件的函数,并在每次有事件时执行它们DOM 的改变
From my experience , the only way I have found to do this is by using livequery
It has a similar syntax, and in your case to bind uploadify on a live element, you would use
Livequery accepts functions without events, and executes them everytime there is a change in the DOM
元素是如何生成的?如果它使用 jQuery 从服务器获取,您可以使用更黑客的方式来修复它,只需将 jQuery 在它遇到的任何脚本标签上运行 eval() ,这样您就可以将:
在获取的 html 中,它会将其绑定到加载而不是试图实时观看它。奖励积分,如果你再次获取 html,它将被解除绑定。
How is the element generated? If its fetched from the server using jQuery you can use a more hackish way of fixing it, simply put jQuery runs eval() on any script tags it runs into so you could just put:
In the fetched html and it'll bind it on load instead of trying to watch over it live. Bonus points, if you fetch the html again it'll be unbound.