jQuery 中复制元素的绑定问题

发布于 2024-08-06 19:58:00 字数 1018 浏览 5 评论 0原文

这实际上是一个更大的问题,因为我知道有多种方法可以解决这个问题,但我会尝试总结一下。

我尝试做的事情:我正在使用这个 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() 的语法相同。有没有类似于绑定但适用于活动元素的东西?

  1. 如果我不尝试替换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?

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还在原地等你 2024-08-13 19:58:00

您可以考虑更改 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.

佞臣 2024-08-13 19:58:00

您可以重载 live 方法,使其支持 data 作为第二个参数:

jQuery.fn.live = (function(_live){
    return function( type, data, fn ) {

        var _fn;

        if ( jQuery.isFunction(fn) ) {
            _fn = function(e) {
                e.data = data;
                return fn.call( this, e );
            };
        }

        return _live.call( this, type, _fn || fn || data );
    };
})(jQuery.fn.live);

替换 data 的所有实例>bind(...)live(...) 现在应该可以工作了。

注意:您必须将重载方法放在其他所有方法之上。

You could overload the live method, making it support data as the second parameter:

jQuery.fn.live = (function(_live){
    return function( type, data, fn ) {

        var _fn;

        if ( jQuery.isFunction(fn) ) {
            _fn = function(e) {
                e.data = data;
                return fn.call( this, e );
            };
        }

        return _live.call( this, type, _fn || fn || data );
    };
})(jQuery.fn.live);

Replacing all instances of bind(...) with live(...) should now work.

Note: you'll have to put the overloaded method above everything else.

浪荡不羁 2024-08-13 19:58:00

根据我的经验,我发现做到这一点的唯一方法是使用 livequery

它具有类似的语法,在您的情况下要在实时元素上绑定 uploadify,您将使用

$('#fileInput').livequery(function(){
  $(this).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

$('#fileInput').livequery(function(){
  $(this).uploadify();
})

Livequery accepts functions without events, and executes them everytime there is a change in the DOM

水染的天色ゝ 2024-08-13 19:58:00

元素是如何生成的?如果它使用 jQuery 从服务器获取,您可以使用更黑客的方式来修复它,只需将 jQuery 在它遇到的任何脚本标签上运行 eval() ,这样您就可以将:

<script type='text/javascript'>
$(function(){
    $('#fileInput').uploadify();
});
</script>

在获取的 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:

<script type='text/javascript'>
$(function(){
    $('#fileInput').uploadify();
});
</script>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文