如何獲取按鈕文本或其 innerHTML 并在頁面內使用它,而不是使用它的“名稱”
<button onclick="myFunction()">my text</button>
<script>
function myFunction() {
alert(my.text or innerHTML);
}
</script>
uj5u.com熱心網友回復:
您可以使用innerText它,如下所示:
document.getElementById('myButton').addEventListener('click', e => {
alert(e.target.innerText)
})
<button id="myButton">
Test button
</button>
uj5u.com熱心網友回復:
屬性中的函式onclick可以使用 將元素作為引數傳遞給函式this。然后直接從它參考該元素的innerTextor屬性,如代碼段所示。innerHTML
<button onclick="myFunction(this)">my text</button>
<script>
function myFunction(element) {
alert(element.innerText);
}
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/479685.html
標籤:html
