检测是否在整个文档上按下了一个按钮

Detecting if a button has been pressed on the whole document

我想检测文档上是否按下了按钮。这意味着我不想为每个按钮输入 document.getElementById("ID").onclick。换句话说,我想检测文档上是否按下了任何按钮。如果按下按钮,获取该按钮对象,获取它的 textContent,仅此而已。

你试过吗?

let buttons = document.querySelectorAll(".button");

buttons.forEach(btn=>{
    btn.addEventListener("click",function(e){
    btn.innerText = "CLICKED BUTTON"
    });
});
<button class="button">1</button>
<button class="button">2</button>
<button class="button">3</button>
<button class="button">4</button>