扩展jQuery插件以更改配置
Extend jQuery plugin to change configuration
我创建了一个简单的jQuery插件来将元素更改为蓝色,并可以向它传递一个参数来将元素改为给定的颜色。
我现在希望创建一个新的插件,将颜色更改为黄色,而不必将颜色作为参数传递。我读过https://stackoverflow.com/a/4414356/1032531,但它似乎描述了向对象添加方法。
这是如何最好地完成的?
http://jsbin.com/xedohu/1/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Extend jQuery Plugin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
(function($){
var defaults = {
'color' : 'blue'
};
var methods = {
init : function (options) {
var settings = $.extend({}, defaults, options);
return this.each(function () {
$(this).css('color',settings.color);
});
},
destroy : function () {
return this.each(function () {});
}
};
$.fn.changeColor = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.changeColor');
}
};
}(jQuery)
);
jQuery.fn.extend({
changeColorToYellow: function() {
//What do I do?
//$.changeColor({color:'yellow'})
}
});
$(function() {
$('#myElement1').changeColor();
$('#myElement2').changeColor({color:'red'});
$('#myElement3').changeColorToYellow();
});
</script>
</head>
<body>
<p id='myElement1'>myElement1</p>
<p id='myElement2'>myElement2</p>
<p id='myElement3'>myElement3</p>
</body>
</html>
您可以在changeColorToYellow中使用this.changeColor({color:'yellow'})。不提供任何选项将使您的插件做一件正确的事情。只要你不打算创建多个插件,只是想用不同的颜色做同样的事情,这是可以的。
代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Extend jQuery Plugin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
(function($){
var defaults = {
'color' : 'blue'
};
var methods = {
init : function (options) {
var settings = $.extend({}, defaults, options);
return this.each(function () {
$(this).css('color',settings.color);
});
},
destroy : function () {
return this.each(function () {});
}
};
$.fn.changeColor = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.changeColor');
}
};
}(jQuery)
);
jQuery.fn.extend({
changeColorToYellow: function() {
this.changeColor({color: 'yellow'});
}
});
$(function() {
$('#myElement1').changeColor();
$('#myElement2').changeColor({color:'red'});
$('#myElement3').changeColorToYellow();
});
</script>
</head>
<body>
<p id='myElement1'>myElement1</p>
<p id='myElement2'>myElement2</p>
<p id='myElement3'>myElement3</p>
</body>
</html>
相关文章:
- 扩展jQuery插件以更改配置
- jquery.final-countdown.js-需要帮助配置它
- jquery-plugin-1配置设置从另一个配置设置获取值
- 如何在 JQuery TE 工具栏中仅配置我需要的选项
- 是否可以将 JQuery “取消滑块”配置为仅更改背景
- 动态加载 JQuery 插件配置
- 在jQuery中使用.click函数将HTML按钮定向到不同的配置文件
- jQuery CarouFredSel 插件触发配置
- 在 JavaScript 或 jQuery 中制作用户可配置的函数
- 渲染后,带有jquery包装器的元素不会被重新配置
- 在多个目标和具有类似配置的页面上使用jQuery插件
- 使用mysql进行初始配置的Javascript/Jquery
- 我必须在哪里插入Keith Wood本地化的jQuery倒计时插件在Wordpress中的配置代码
- 硬币滑块jquery-->如何将100%width参数传递给它?似乎只允许图像的像素配置
- 如何在使用JQuery追加HTML元素之后配置它们
- jquery库”;uriAnchor”-我可以'我不知道如何设置验证配置
- 无法在回调时配置选项(jQuery插件)
- 使用特定版本的JQuery库需要JS配置
- 配置 jQuery 以在 ajax 调用失败时重试
- 配置 jQuery 滑块