1 回答

TA貢獻1775條經驗 獲得超8個贊
現在,您正在為每個按鈕創建一個按鈕:
let C5 = document.createElement ('button');
C5.innerHTML = '<button id="C5">C5</button>';
這就像創造一些東西
<button><button id="C5">C5</button></button>
這就是造成問題的原因。
相反,分配給您創建的按鈕的id和屬性:textContent
sibemol.id = 'A#4'; // you're already doing this with setAttribute below
sibemol.textContent = 'Bb4';
C5.id = 'C5';
C5.textContent = 'C5';
D5.id = 'D5';
D5.textContent = 'D5';
// etc
(不需要setAttribute,它不必要地冗長)
或者,如果 ID 與 textContent 匹配,則不再重復:
const keys = ['G4', 'Bb4', 'C5', 'D5', 'F5', 'G5'];
for (const key of keys) {
const button = keyboard.appendChild(document.createElement('button'));
button.id = key;
button.textContent = key;
}
- 1 回答
- 0 關注
- 150 瀏覽
添加回答
舉報