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