關于n的次數問題 ???? var n = 0; //綁定事件 $(".aaron:last").on('mousedown mouseup', function(e) { $(this).text( '觸發類型:' + (e.type) + ",次數" + ++n) ++n; })
為什么點擊后松開 n是3而不是2???
當我寫這樣的時候,顯示的n的結果是2
$(this).text( '觸發類型:' + ?(e.type) + ",次數" + ++n)
老師的源碼寫這樣的時候,顯示的n的結果是3。結尾多了++n 是想說明什么?
?$(this).text( '觸發類型:' + ?(e.type) + ",次數" + ++n)?++n;
<h4>測試一</h4>
? ? <div class="left">?
? ? ? ? on('mousedown mouseup')
? ? ? ? <div class="aaron">點擊觸發</div>
? ? </div>
? ? <button>點擊銷毀所有事件off</button>
? ? <script type="text/javascript">
? ? var n ?= 0;
? ? //綁定事件
? ? $(".aaron:last").on('mousedown mouseup', function(e) {
? ? ? ? $(this).text( '觸發類型:' + ?(e.type) + ",次數" + ++n)
? ? ? ? ++n;
? ? })
? ? //刪除事件
? ? $("button:last").click(function() {
? ? ? ? $(".aaron:last").off()
? ? })
? ?
? ? </script>
2017-07-17
你的順序有些問題,先是n==1,顯示,然后n==2,這都是mousedown事件的執行過程。
$(this).text( '觸發類型:' + ?(e.type) + ",次數" + ++n)
? ? ? ? ++n;
仔細看你的源碼,你再++n一次之后就輸出了,所以在顯示了1之后mousedown事件還沒有結束,會接著執行++n;之后才會結束,這時候n==2。之后你松開了鼠標觸發了mouseup事件。這時也是先執行$(this).text( '觸發類型:' + ?(e.type) + ",次數" + ++n)這句。所以顯示的是(在n==2之后)++n的值也就是3。之后執行++n;n==4結束mouseup事件。
2018-10-22
先是n==0,不是1
2017-07-17
當你按下鼠標時觸發mousedown,++n,n==1,顯示1,之后++n,n==2,松開鼠標時觸發mouseup,++n,n==3,顯示3,之后++n,n==4.