Я пытаюсь работать на chainability в JQuery плагин, и он прекрасно работает с JQuery click
below,JQuery плагина с каждым и готов
$(document).ready(function(){
$('.call-parent').parent_child({
target: '.element'
});
$('.call-child-1').click(function(){
$(this).parent_child().child_1();
return false;
});
$('.call-child-2').click(function(){
$(this).parent_child().child_2();
return false;
});
});
(function($) {
$.fn.extend({
parent_child: function(options) {
var defaults = {
target:''
}
var options = $.extend(defaults, options);
var o = options;
var $this = this;
var $cm = this.click(function(ei) {
alert('parent');
$this.child_1();
return false;
});
$this.child_1 = function() {
alert('child 1');
};
$this.child_2 = function() {
alert('child 2');
};
return $cm;
}
})
})(jQuery);
, но у меня есть ошибки, когда я использую each
или ready
в плагине, например,
$(document).ready(function(){
$('.call-parent').parent_child({
target: '.element'
});
$('.call-child-1').click(function(){
$(this).parent_child().child_1();
return false;
});
$('.call-child-2').click(function(){
$(this).parent_child().child_2();
return false;
});
});
(function($) {
$.fn.extend({
parent_child: function(options) {
var defaults = {
target:''
}
var options = $.extend(defaults, options);
var o = options;
var $this = this;
var $cm = this.each(function(ei) {
alert('parent');
$this.child_1();
return false;
});
$this.child_1 = function() {
alert('child 1');
};
$this.child_2 = function() {
alert('child 2');
};
return $cm;
}
})
})(jQuery);
сообщение об ошибке,
$ this.child_1 не является функцией [Перерыв на этой ошибке]
Почему я не могу сделать это с each
или ready
? Или я сделал это неправильно?
выражения функций не поднимаются, поэтому, когда вы устанавливаете '$ cm',' $ this.child_1' еще не существует. – Mathletics
получил. Спасибо за ответ. – laukok