比如我寫了:$("input:checkbox:checked").each(function(){alert($(this).val())})是OK的,但是:$("input:checkbox:checked").each(function(){alert($("this").val())})就報錯了?
4 回答

大話西游666
TA貢獻1817條經驗 獲得超14個贊
$this只是個自定義的變量;
$(this)是將當前don對象轉換成jquery對象 ;
一般定義jquery變量習慣$開頭,提高可讀性而已。
$(this)是把DOM對象封裝成jquery對象,其相當于一個集合。
用法如下:
<a href="http://IP/q/1010000000125418" target="_blank" data-id="1010000000125418">jQuery</a>
$('a').click(function(){
this.innerHTM==$(this).html()=='jQuery';//三者是一樣的.
this.getAttribute('href')==this.href==$(this).attr('href')//三者是一樣的;
this.getAttribute('target')==this.target==$(this).attr('target')//三者是一樣的;
this.getAttribute('data-id')==$(this).attr('data-id')//二者是一樣的;
});
- 4 回答
- 0 關注
- 551 瀏覽
添加回答
舉報
0/150
提交
取消