jquery 高亮插件写法


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>test-plugin</title>
<meta name="author" content="v.wupingteng">
<script src="jquery/jquery-1.6.2.min.js"></script>
<script src="jquery/external/jquery.metadata.js"></script>
<script >
(function($) {
// hilight插件的定义
$.fn.hilight = function(options) {
//插件的默认配置
var defaults = {
foreground: 'red',
background: 'yellow'
};
var opts = $.extend({}, defaults, options);

//遍历每个元素
return this.each(function() {
$this = $(this);
var o = $.metadata ? $.extend({}, opts, $this.metadata()) : opts;
$this.css({
backgroundColor: o.background,
color: o.foreground
});
var markup = $this.html();
markup = $.fn.hilight.format(markup);
$this.html(markup);
});
};

// 私有函数:debugging
function debug(msg) {
if (window.console && window.console.log)
window.console.log(msg);
};

// 暴露format函数
$.fn.hilight.format = function(txt) {
return '<strong>' + txt + '</strong>';
};

})(jQuery);

//当DOM完全加载完成时执行初始化
(function($){
$(function(){
$('.hilight').hilight();
});
})(jQuery);
</script>
</head>
<body>
<div class="hilight { background: 'red', foreground: 'white' }">
背景红色,文字白色
</div>
<div class="hilight { foreground: 'orange' }">
背景默认颜色 ,文字橙色
</div>
<div class="hilight { background: 'green' }">
背景绿色,文字默认颜色
</div>

<li class="someclass" data="{some:'random', json: 'data'}">...</li>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值