首先來看看JQuery中的 $() 這個符號,實際上這個符號在JQuery中至關於JQuery(),即$(this)=jquery();也就是說,這樣能夠返回一個jquery對象。那麼,當你在網頁中alert($('#id'));時,會彈出一個[object Object ],這個object對象,也就是jquery對象了。
那麼,咱們再回過頭來講$(this),這個this是什麼呢?假設咱們有以下的代碼:html
<input id="jq" type="text" value=1>
$("#jq").click(function(){ alert($(this)); // [object Object ] alert(this); // [object HTMLInputElement] console.log($(this)); // r.fn.init [input#jq] console.log(this); // <input id="jq" type="text" value="1"> console.log(this.value); // 1 console.log($(this).value); // undefined console.log($(this).children()); // r.fn.init [prevObject: r.fn.init(1)] console.log(this.children()); // Uncaught TypeError: this.children is not a function });
也就是說,this返回的是一個html對象,也就是節點,他能夠獲取到屬性值,可是html對象不具備function方法,天然會報錯,所以須要使用$(this)纔可以調用方法。jquery