jQuery中$this和$(this)的區(qū)別介紹(一看就懂)
更新時(shí)間:2015年07月06日 09:29:05 投稿:junjie
這篇文章主要介紹了jQuery中$this和$(this)的區(qū)別介紹(一看就懂),本文用簡潔的語言講解了它們之間的區(qū)別,并給出了一個(gè)例子來說明,需要的朋友可以參考下
// this其實(shí)是一個(gè)Html 元素。 // $this 只是個(gè)變量名,加$是為說明其是個(gè)jquery對(duì)象。 // 而$(this)是個(gè)轉(zhuǎn)換,將this表示的dom對(duì)象轉(zhuǎn)為jquery對(duì)象,這樣就可以使用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其實(shí)是一個(gè)Html 元素。 // $this 只是個(gè)變量名,加$是為說明其是個(gè)jquery對(duì)象。 // 而$(this)是個(gè)轉(zhuǎn)換,將this表示的dom對(duì)象轉(zhuǎn)為jquery對(duì)象,這樣就可以使用jquery提供的方法操作。 $this = $(this); // build element specific options var o = $.meta ? $.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 '<strong>' + txt + '</strong>'; }; // 插件的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)
您可能感興趣的文章:
- jQuery AJAX回調(diào)函數(shù)this指向問題
- jQuery 選擇方法及$(this)用法實(shí)例分析
- 淺談jQuery this和$(this)的區(qū)別及獲取$(this)子元素對(duì)象的方法
- 實(shí)例講解JQuery中this和$(this)區(qū)別
- 詳談jQuery中的this和$(this)
- 通過$(this)使用jQuery包裝后的方法或?qū)傩?/a>
- jquery $(this).attr $(this).val方法使用介紹
- jquery中this的使用說明
- JQuery this 和 $(this) 的區(qū)別
- JQuery中this的指向詳解
相關(guān)文章
jQuery 添加樣式屬性的優(yōu)先級(jí)別方法(推薦)
下面小編就為大家?guī)硪黄猨Query 添加樣式屬性的優(yōu)先級(jí)別方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06google jQuery 引用文件,jQuery 引用地址集合(jquery 1.2.6至jquery1.5.2)
很多網(wǎng)站都是使用這種方式引入,客戶的瀏覽器可能已經(jīng)緩存過了 jquery??梢灾苯诱{(diào)用本地的,速度更快2011-04-04jquery實(shí)現(xiàn)全選、反選、獲得所有選中的checkbox
這篇文章主要介紹了jquery實(shí)現(xiàn)全選、反選、獲得所有選中的checkbox功能,對(duì)checkbox感興趣的小伙伴們可以參考一下2015-12-12jquery創(chuàng)建并行對(duì)象或者合并對(duì)象的實(shí)現(xiàn)代碼
如果有對(duì)象A ,B 現(xiàn)在我想要合并成對(duì)象C 從C里面可以找到A , B 及其子對(duì)象 怎么做2012-10-10分享2個(gè)jQuery插件--jquery.fileupload與artdialog
這篇文章主要介紹了分享2個(gè)jQuery插件--jquery.fileupload與artdialog的使用方法及示例,非常的實(shí)用,這里推薦給大家。2014-12-12