jQuery自定義事件之trigger事件這一篇有人可以幫忙解釋下嗎
?//點擊更新次數(shù)
??? $("button:first").click(function(event,bottonName) {
??????? bottonName = bottonName || 'first';
??????? update($("span:first"),$("span:last"),bottonName);
??? });
??? //通過自定義事件調(diào)用,更新次數(shù)
??? $("button:last").click(function() {
??????? $("button:first").trigger('click','last');
??? });
??? function update(first,last,bottonName) {
??????? first.text(bottonName);
??????? var n = parseInt(last.text(), 10);
??????? last.text(n + 1);
??? }
2017-07-19
$("button:first").click(function(e){
? ? ? ? var n = parseInt($("span:last").text());
? ? ? ? $("span:last").text(n + 1);
? ? });
? ? $("button:last").click(function(){
? ? ? ? $("button:first").trigger('click');
? ? });
第一個按鈕綁定了,單擊事件
單擊第二個 按鈕 時 調(diào)用 第一個的單擊事件(trigger就是用來調(diào)用某個節(jié)點的某個事件)
2017-06-21
還是暈乎乎的!
2017-04-21
引用“學(xué)徒王小明"的回答:
當(dāng)點擊第一個 button 時,會觸發(fā)第一個按鈕的 click事件,function 參數(shù)中的 bottonName,此時還未定義(undefined),所以在執(zhí)行"或"語句時,將 'first'這個字符串賦給bottonName,繼續(xù)執(zhí)行 update()函數(shù)。第一個參數(shù)為第一個 span對象,第二個參數(shù)為第二個 span對象,第三個參數(shù)的內(nèi)容此時為 'first'。在update函數(shù)中,將第一個span的文本設(shè)置為 'first',將第二個span的文本轉(zhuǎn)換為整型,基數(shù)為10,計數(shù)器n自加 1,并賦給第二個 span的文本
點擊第二個 button的過程以此類推。