亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jQuery中$this和$的區別介紹

jQuery中$this和$的區別介紹

桃花長相依 2018-11-27 01:00:55
jQuery中$this和$的區別介紹
查看完整描述

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)


 


查看完整回答
反對 回復 2018-12-23
  • 1 回答
  • 0 關注
  • 474 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號