jQuery中$this和$(this)的区别介绍一看就懂

<div class="jb51code">
<pre class="brush:js;">
// this其实是一个Html 元素。
// $this 只是个变量名,加$是为说明其是个jquery对象。
// 而$(this)是个转换,将this表示的dom对象转为jquery对象,这样就可以使用jquery提供的方法操作。

(function($){
$.fn.hilight = function(options){
debug(this);

    var defaults = {
        foreground: 'red',background: 'yellow'
    };

    var opts = $.extend({},$.fn.hilight.defaults,options);

    return this.each(function() {
  // this其实是<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>Html 元素。
  // $this 只是个变量名,加$是为说明其是个jquery对象。
  // 而$(this)是个转换,将this表示的dom对象转为jquery对象,这样就可以使用jquery提供的<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>操作。
        $this = $(this);

        // build element specific options
        var o = $.<a href="https://www.jb51.cc/tag/Meta/" target="_blank" class="keywords">Meta</a> ? $.extend({},opts,$this.data()) : opts;

        // update element styles
        $this.css({
            backgroundColor: o.background,color: o.foreground
        });

        var markup = $this.html();
        // call our format function

        markup = $.fn.hilight.format(markup);

        $this.html(markup);
    });

};


// define our format function
$.fn.hilight.format = function(txt) {
    return '<h3>' + txt + '</h3>';
};


// <a href="https://www.jb51.cc/tag/chajian/" target="_blank" class="keywords">插件</a>的defaults
$.fn.hilight.defaults = {
    foreground: 'red',background: 'yellow'
};

function debug($obj) {
    if (window.console && window.console.log){
        window.console.log('hilight selection count: ' + $obj.size());
    }
};

})(jQuery)

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...