我有一個插件(稱爲可插拔),其採用任何.plugin
,並增加了它這個標記我可以在同一個插件
<span class="colorable">color me</span>
這是我原來的標記帶class =「插件」中添加此代碼。
<div class="plugin"></div>
<div class="plugin"></div>
<div class="plugin"></div>
在JS,我叫$('.plugin').pluggable();
它增加了上面的<span>
HTML和使它看起來像這樣
<div class="plugin"><span class="colorable">color me</span></div>
<div class="plugin"><span class="colorable">color me</span></div>
<div class="plugin"><span class="colorable">color me</span></div>
這是所有的插件不(添加此HTML)和插件本身是簡單的像這
(function($){
$.fn.pluggable = function() {
return this.each(function() {
$(this).html('<span class="colorable">color me</span>');
});
};
})(jQuery);
但是我想真正做的是,在這之後,點擊帶班.colorable跨度的時候,我希望它改變其背景顏色重新d。我可以用jQuery做類似這樣的
$('.colorable').click(function(){
$this.css("background-color", "orange");
});
,但有沒有辦法讓整個操作是自包含有包含在同一插件的代碼。我不知道,因爲該插件運行在原始元素是.plugin
類,但最終得到的彩色元素是被添加.colorable
新跨越的標記。但是我想將這個操作的所有代碼保存在一個插件/文件中。可能嗎?