Issue with binding in jQuery for copied elements

我怕爱的太早我们不能终老 提交于 2019-12-06 12:59:53

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.

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!