【发布时间】:2011-02-05 16:40:41
【问题描述】:
我有一个插件(称为pluggable),它接受任何.plugin 并添加这个标记
<span class="colorable">color me</span>
这是我使用 class="plugin" 的原始标记。
<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 的跨度时,我希望它的背景颜色更改为红色。我可以像这样用jquery做到这一点
$('.colorable').click(function(){
$this.css("background-color", "orange");
});
但是有没有办法让这个代码包含在同一个插件中,以便整个操作是自包含的。我想知道,因为插件操作的原始元素是类.plugin,但最终着色的元素是添加.colorable 的新跨度标记。但我想将此操作的所有这些代码保存在一个插件/文件中。是否可以?
【问题讨论】:
标签: javascript jquery plugins jquery-plugins