0
<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Demo</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
            var headings = $('h3');
            var paras = $('p');
            paras.hide().eq(0).show();
            headings.click(function() {
                var cur = $(this); //save the element that has been clicked for easy referal
                cur.siblings('p').hide(); //hide all the paragraphs
                cur.next('p').show(); //get the next paragraph after the clicked header and show it
            });
        </script>
        <style type="text/css">
            p,h3 {margin: 0; padding: 0;}
            p {height: 150px; width: 200px; border: 1px solid black;}
            h3 {height: 50px; width: 200px; background-color: blue; color: white; border: 1px solid black;}
        </style>
    </head>
    <body>
        <h3>Item 1 </h3>
        <p>Item 1 content</p>
        <h3>Item 2 </h3>
        <p>Item 2 content</p>
        <h3>Item 3 </h3>
        <p>Item 3 content</p>
        <h3>Item 4</h3>
        <p>Item 4 content</p>
    </body>    
</html>

上記のコードはここから取得されます: http://www.1stwebdesigner.com/tutorials/jquery-for-complete-beginners-part-3/

質問:

この行の場合:クリックされたことを意味することはvar cur = $(this);わかっていますが、なぜこのように書けないのでしょうか:とここの違いは何ですか?thish3var cur = this;this$(this)

4

4 に答える 4

5
$(this)

によって返された DOM 要素thisを jQuery オブジェクトに変換します。このオブジェクトから、引き続き jQuery を使用できます。

于 2013-06-19T02:47:42.517 に答える
2

this現在の関数を呼び出すメンバーへの参照です

例えば

$(document).ready(function(){
    $("#test").click(function(){

var jQuerizedElement = $( this ); // instead of calling it by its selector you can use this
  });
});

これで、より多くのことができます。

于 2013-06-19T02:48:22.200 に答える
2

thisDOM 要素を$(this)返しますが、jQuery オブジェクトを返します。

this

と同等です

$(this).get()
于 2013-06-19T02:48:35.937 に答える
0

$jQuery 関数の省略形です。次のように書くこともできます。

var cur = jQuery(this);

jQuery は、DOM 要素やその他のオブジェクトを「ラップ」して、より多くの機能を提供します。セレクター文字列を jQuery コンストラクターに渡すのと同じように、ネイティブ DOM 要素 (つまりthis) を渡すことができ、それが jQuery オブジェクトになります。

于 2013-06-19T02:48:43.577 に答える