jQuery 插件:重新附加插件本身失败

发布于 2024-12-02 02:54:46 字数 3142 浏览 0 评论 0原文

我有这个 html:

<div class="item">
    <ul>
        <li><a href="form.php" class="delete">delete</a></li>
    </ul>
</div>

<div class="item">
    <ul>
        <li><a href="form.php" class="delete">delete</a></li>
    </ul>
</div>

<div class="item">
    <ul>
        <li><a href="form.php" class="delete">delete</a></li>
    </ul>
</div>

当您单击删除按钮时,将加载一个表单,要求您单击

当您点击时,将返回之前的 html。

我在下面制作的这个插件的问题是,您最多只能单击删除按钮两次次,然后ajax在第三次时无法工作强>时间。

怎么会?如何解决这个问题?

您可以在此链接上看到此错误。

jQuery:

$(document).ready(function(){
    $(".delete").delete_string({targetElement:'.item'});
});

(function($){
    // Attach this new method to jQuery
    $.fn.extend({ 
        // This is where you write your plugin's name
        delete_string: function(options) {
            // Set the default values, use comma to separate the settings, example:
            var defaults = {
                targetElement:          '.item-needle',
                targetSlibing:          false
            }

            var options =  $.extend(defaults, options);
            var o = options;

            // When you create the click function you can assign that element to a variable and reference it within:
            var $cm = this.click(function(e){

                // Set the varible.
                var object = $(this);
                var object_path = object.attr('href');
                var object_parent = object.parents('ul');
                var html_parent = object_parent.html();
                var object_superparent = object.parents(o.targetElement);

                var target_element = object.parents(o.targetElement);
                var target_slibing = target_element.next(o.targetSlibing);
                //alert(target_slibing);

                // Load the form.
                object_parent.load( object_path, function(){

                    // Attach click to the no button.
                    $("input[type=button][name=no]",object_parent).click(function(){

                        // Get the html content back.
                        object_parent.html(html_parent);

                        // Attach the delete plugin back in.
                        $($cm.selector,object_parent).delete_string({targetElement:o.targetElement});
                        return false;

                    });
                });
                return false;
            });
        }
    });
})(jQuery);

据我所知,问题来自这一行:

$($cm.selector,object_parent).delete_string({targetElement:o.targetElement});

如果我这样做就好了,

$($cm.selector).delete_string({targetElement:o.targetElement});

但这会将删除插件附加到所有其他现有的删除按钮在屏幕上,不是吗?

I have this html:

<div class="item">
    <ul>
        <li><a href="form.php" class="delete">delete</a></li>
    </ul>
</div>

<div class="item">
    <ul>
        <li><a href="form.php" class="delete">delete</a></li>
    </ul>
</div>

<div class="item">
    <ul>
        <li><a href="form.php" class="delete">delete</a></li>
    </ul>
</div>

When you click on the delete button, a form will be loaded to ask you to click yes or no.

When you click no the previous html will be returned.

The problem in this plugin I made below, is that you only can click the delete button up to two times then the ajax fails to work at the third time.

How come? How can resolve this?

You can see this bug on this link.

The jQuery:

$(document).ready(function(){
    $(".delete").delete_string({targetElement:'.item'});
});

(function($){
    // Attach this new method to jQuery
    $.fn.extend({ 
        // This is where you write your plugin's name
        delete_string: function(options) {
            // Set the default values, use comma to separate the settings, example:
            var defaults = {
                targetElement:          '.item-needle',
                targetSlibing:          false
            }

            var options =  $.extend(defaults, options);
            var o = options;

            // When you create the click function you can assign that element to a variable and reference it within:
            var $cm = this.click(function(e){

                // Set the varible.
                var object = $(this);
                var object_path = object.attr('href');
                var object_parent = object.parents('ul');
                var html_parent = object_parent.html();
                var object_superparent = object.parents(o.targetElement);

                var target_element = object.parents(o.targetElement);
                var target_slibing = target_element.next(o.targetSlibing);
                //alert(target_slibing);

                // Load the form.
                object_parent.load( object_path, function(){

                    // Attach click to the no button.
                    $("input[type=button][name=no]",object_parent).click(function(){

                        // Get the html content back.
                        object_parent.html(html_parent);

                        // Attach the delete plugin back in.
                        $($cm.selector,object_parent).delete_string({targetElement:o.targetElement});
                        return false;

                    });
                });
                return false;
            });
        }
    });
})(jQuery);

The problem come from this line as far as I can find out:

$($cm.selector,object_parent).delete_string({targetElement:o.targetElement});

it would be fine if I just do this,

$($cm.selector).delete_string({targetElement:o.targetElement});

but this will attach the delete plugin to all other existing delete button on the screen, won't it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绅刃 2024-12-09 02:54:46

当为多个元素构建插件时,最好返回 $.each:

(function($){   
    $.fn.delete_string=function(options){
        var defaults={
            targetElement:'.item-needle',
            targetSlibing:false
        },options=$.extend(defaults,options);
        return($(this).each(function(){
            // Build your plug-in here
        }));
    };
})(jQuery);

When building a plug-in for multiple elements, it is always best to return $.each:

(function($){   
    $.fn.delete_string=function(options){
        var defaults={
            targetElement:'.item-needle',
            targetSlibing:false
        },options=$.extend(defaults,options);
        return($(this).each(function(){
            // Build your plug-in here
        }));
    };
})(jQuery);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文