jQuery中$this和$(this)的区别介绍(一看就懂)
//this其实是一个Html元素。
//$this只是个变量名,加$是为说明其是个jquery对象。
//而$(this)是个转换,将this表示的dom对象转为jquery对象,这样就可以使用jquery提供的方法操作。
(function($){
$.fn.hilight=function(options){
debug(this);
vardefaults={
foreground:'red',
background:'yellow'
};
varopts=$.extend({},$.fn.hilight.defaults,options);
returnthis.each(function(){
//this其实是一个Html元素。
//$this只是个变量名,加$是为说明其是个jquery对象。
//而$(this)是个转换,将this表示的dom对象转为jquery对象,这样就可以使用jquery提供的方法操作。
$this=$(this);
//buildelementspecificoptions
varo=$.meta?$.extend({},opts,$this.data()):opts;
//updateelementstyles
$this.css({
backgroundColor:o.background,
color:o.foreground
});
varmarkup=$this.html();
//callourformatfunction
markup=$.fn.hilight.format(markup);
$this.html(markup);
});
};
//defineourformatfunction
$.fn.hilight.format=function(txt){
return'<strong>'+txt+'</strong>';
};
//插件的defaults
$.fn.hilight.defaults={
foreground:'red',
background:'yellow'
};
functiondebug($obj){
if(window.console&&window.console.log){
window.console.log('hilightselectioncount:'+$obj.size());
}
};
})(jQuery)
热门推荐
10 烟酒店开业祝福语简短
11 便利贴老师祝福语简短
12 买车新年祝福语简短创意
13 同学贺卡励志祝福语简短
14 新年祝福语简短创意 英语
15 学弟英语高考祝福语简短
16 雷州人结婚祝福语简短
17 儿子立冬祝福语简短独特
18 喝酒祝福语大全简短新颖