# Javascript/jQ this $(this)是什麼 ----- ###### tags: `javascript` `jQuery` 在寫到radio 點擊取消選取時 看到的這樣的寫法 想說都把this給 賦值變量了 怎麼在if 裡面還寫成this 給變量值後就要使用這麼變量啊 就把if內的code改寫 結果會動的變不會動了!! 兩個consloe.log出來才知道 根本不一樣!!!! ```javascript= var $this = $(this); if ($this.data('checked')) { this.checked = false; // $this.checked = false; } console.log(this); console.log($this); ``` ![](https://i.imgur.com/pQt9eU0.png) ## 是什麼? 簡單來說 this 是個元素 $(this) 是jQ函式 但是傳this給函式 也有人說 \$(this)是jQ的對象 this是DOM元素對象 但我覺得解釋成 函式 更貼切 ![](https://i.imgur.com/7Hliu7X.png) ![](https://i.imgur.com/n6KU9lj.png) $(this) is a jQuery object and this is a pure DOM Element object. See this example: ```javascript= $(".test").click(function(){ alert($(this).text()); //and alert(this.text()); // error no method }); ``` [\$(this) vs this in JavaScript](https://stackoverflow.com/questions/20718112/this-vs-this-in-javascript) --- ## 實作練習 [jQuery 中的 this 與 \$(this) 用法差異](https://icodding.blogspot.com/2015/12/jquery-this-this.html) ![](https://i.imgur.com/w9GzH1A.png) 這只是測試程式 並不能更改input radio樣式 所以畫面上樣式還是一樣 但是 實際上已經附加css上去 ![](https://i.imgur.com/0RgBLdu.png) ![](https://i.imgur.com/u7Q2xj9.png)