自定义Jquery插件方法


<html>
<head>
<title>jquery plugins</title>
<style type="text/css">
#main{
width:300px;
height:120px;
}
.main_box{
width:120px;
height:100px;
background:#CCC;
margin:5px;
float:left;
color:#fff;
text-align:center;
}

.test{
background:#000;
}

.test2{
background:#888;
}
</style>
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript">
<!--
//定义作用域:定义一个JQuery插件,首先要把这个插件的代码放在一个不受外界干扰的地方。
(function($){
//内部方法
var setFontColor = function(obj,color){
$(obj).css({"color":color});
}
//定义插件
$.fn.setBackGround = function (options) {
//默认参数
var defaults = {
className:'test'
};
//覆盖defaults中的对应属性,并替换options。可以理解成合并覆盖
options = $.extend(defaults, options);

$(this).addClass(options.className);
//支持链接调用
return this.each(function(){
setFontColor(this,"red");
});
}

})(jQuery);

$(document).ready(function(){
//调用方式
//#main
$(".main_box").setBackGround({className:'test2'})//插件调用
.css({ "border-width": "1", "border-color": "red", "border-bottom-style": "dotted" });//链接调用
});
//-->
</script>
</head>
<body>
<div id="main">
<div class="main_box">a</div>
<div class="main_box">b</div>
<div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值