1 回答

TA貢獻1895條經驗 獲得超7個贊
$ 是JQuery對象,是JQuery 常用的一個回傳函數,定義為 "選取" 英文是 selector 的縮寫例子︰$.function();就是 選取 JQuery 定義的 function() 執行$('input')就是 選取 HTML 當中全部的 input 標簽$('#abc')就是 選取 HTML 當中 ID 名稱為 abc 的物件$.fn.testing = function() {}就是 選取 JQuery 內核函數 fn (函數) 回傳給 testing 這個名稱、定義為一個功能 function()
$this 只是個變量名,加$是為說明其是個jquery對象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | // 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其實是一個Html 元素。 // $this 只是個變量名,加$是為說明其是個jquery對象。 // 而$(this)是個轉換,將this表示的dom對象轉為jquery對象,這樣就可以使用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) |
- 1 回答
- 0 關注
- 474 瀏覽
添加回答
舉報