Javascript 我可以按id或按类选择多个html元素吗?
我在html文档中有4个按钮,我想根据函数的值更改所有按钮的文本内容。我一次只能为一个人做这件事,但不是为所有人 我曾尝试使用getElementById以特定元素为目标,并用逗号分隔ID,但这无法解决问题Javascript 我可以按id或按类选择多个html元素吗?,javascript,html,dom,Javascript,Html,Dom,我在html文档中有4个按钮,我想根据函数的值更改所有按钮的文本内容。我一次只能为一个人做这件事,但不是为所有人 我曾尝试使用getElementById以特定元素为目标,并用逗号分隔ID,但这无法解决问题 //This is the only example that will actually change the text content for only one of the 4 buttons randomCapital.then(value => {
//This is the only example that will actually change the text content for
only one of the 4 buttons
randomCapital.then(value => {
const btnSelect4Reneder = document.querySelector('#btn1')
btnSelect4Reneder.textContent = value
console.log(value)
})
//This will not fix the problem, none of the buttons will change their text
content
randomCapital.then(value => {
const btnSelect4Reneder =
document.getElementByClassName('btns')
btnSelect4Reneder.textContent = value
console.log(value)
})
//As expected, this way will not work either
randomCapital.then(value => {
const btnSelect4Reneder = document.querySelector('#btn1, #btn2, #btn3, #btn4')
btnSelect4Reneder.textContent = value
console.log(value)
})
您可以使用jQuery。依班选择是这种情况下的做法,伊姆霍
$('.btns').text(value)
设btns=document.getElementsByClassName'btn';
让字符串='新内容';
对于let i=0;i