jQuery 插件:重新附加插件本身失败
我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当为多个元素构建插件时,最好返回 $.each:
When building a plug-in for multiple elements, it is always best to return $.each: