我創建了一個jQuery插件,現在我想其綁定到DOM這是由PHP腳本創建的元素。jQuery插件 - 動態綁定到元素
使用下面的標記爲例:
<div class="showcase_window">
<div id='mywhatever'></div>
<div id='mywhatever2'></div>
<div id='mywhatever3'></div>
</div>
這工作:
$(document).ready(function(){
$('#mywhatever').showcase();
$('#mywhatever2').showcase();
$('#mywhatever3').showcase();
});
但由於標記是由PHP腳本創建的,我試圖得到這樣的工作:
$(document).ready(function(){
$('.showcase_window').children().on('showcase');
});
但我有點困惑......我明白'on'用於附加事件,但我我不知道如何去了解它...
非常感謝!
P.S:這裏是插件:
$.fn.showcase = function() {
return this.each(function() {
var $this = $(this);
$this.find(".sc_itm_display").hover(function() {
$this.find(".sc_img_front").stop(true,true).fadeOut("slow");
}, function() {
$this.find(".sc_img_front").stop(true,true).fadeIn("slow");
});
$this.find('.sc_color_select img').click(function() {
var color_path_1 = $(this).attr("src").replace("color","1");
var color_path_2 = $(this).attr("src").replace("color","2");
$this.find('.sc_itm_display .sc_img_back img').attr("src",color_path_1);
$this.find('.sc_itm_display .sc_img_front img').attr("src",color_path_2);
});
});
};
'on'僅適用於事件。不用於附加插件的東西。 – gdoron
不知道,但嘗試這個... $( 'showcase_window。')兒童()上。({ 點擊:函數(){ this.showcase();} }); – sree