亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在同一個 div 元素上添加多個 onclick 操作的最佳實踐是什么

在同一個 div 元素上添加多個 onclick 操作的最佳實踐是什么

搖曳的薔薇 2021-11-25 16:08:35
我確實明白這是一種突兀的方式,但我正在實施的方式已經走了很長一段路,所以我現在無法真正改變,我使用的方法有時有效,其他方法只作用于最后的點擊操作,我想要一種模式它可以正常工作,請在代碼之前參考以查看我如何添加 onclick 操作。我想知道為什么它有時有效,而在其他時候卻無法全部執行<button onclick="myfunctionone(); myfunctiontwo(); myfunctionthree();">click here</button>
查看完整描述

2 回答

?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

使用EventTarget.prototype.addEventListener.


像您一樣使用內聯事件偵聽器通常被認為是一種非常糟糕且可能是惡意的做法。


例子:


// make sure the DOM is parsed before trying to find elements to attach listeners

document.addEventListener('DOMContentLoaded', function() {

  // find the element to attach the listeners to

  const btn = document.getElementById('example-button');

  // create an array with references of your handlers

  const handlers = [handler1, handler2, handler3];

  // for each handler in that array, add is as a listener to the element

  for (const handler of handlers) {

    btn.addEventListener('click', handler);

  }

});


function handler1(event) {

  console.log('handler 1 says: ' + event.target.textContent);

}


function handler2(event) {

  console.log('handler 2 says: ' + event.target.tagName);

}


function handler3(event) {

  console.log('handler 3 says: ' + event.target.id);

}

<button id="example-button">Example button, click me</button>


這也允許在需要時刪除任何一個或多個這些偵聽器:


// make sure the DOM is parsed before trying to find elements to attach listeners

document.addEventListener('DOMContentLoaded', function() {

  const btn = document.getElementById('example-button');

  for (const checkbox of document.querySelectorAll('input[type=checkbox][id^=handler]')) {

    checkbox.addEventListener('change', function() {

      const handler = window[this.id.replace('toggle', '')];

      btn[`${this.checked ? 'add' : 'remove'}EventListener`]('click', handler);

    })

  }

});


function handler1(event) {

  console.log('handler 1 says: ' + event.target.textContent);

}


function handler2(event) {

  console.log('handler 2 says: ' + event.target.tagName);

}


function handler3(event) {

  console.log('handler 3 says: ' + event.target.id);

}

[id^=handler] + label::after { content: 'off'; }

[id^=handler]:checked + label::after { content: 'on'; }

<button id="example-button">Example button, click me</button>

<br />

<hr />

<input type="checkbox" id="handler1toggle" /> <label for="handler1toggle">handler 1 </label>

<input type="checkbox" id="handler2toggle" /> <label for="handler2toggle">handler 2 </label>

<input type="checkbox" id="handler3toggle" /> <label for="handler3toggle">handler 3 </label>



查看完整回答
反對 回復 2021-11-25
?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

您也可以使用 Jquery 并執行以下操作:


$("#examle-button").on("click", function(){

    myfunctionone();

    myfunctiontwo();

    myfunctionthree();

})


查看完整回答
反對 回復 2021-11-25
  • 2 回答
  • 0 關注
  • 258 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號