将javascript应用于多个div

将javascript应用于多个div,javascript,html,Javascript,Html,我有如下JavaScript/HTML代码: const priceDiv=document.querySelector'pplices'; priceDiv.textContent=+priceDiv.textContent.toLocaleString'en-US'; 125135135 1361436136 136136对于pass-all元素,您需要使用类而不是id和forEach的queryselectoral const priceDivs=document.querySelec

我有如下JavaScript/HTML代码:

const priceDiv=document.querySelector'pplices'; priceDiv.textContent=+priceDiv.textContent.toLocaleString'en-US'; 125135135 1361436136 136136对于pass-all元素,您需要使用类而不是id和forEach的queryselectoral

const priceDivs=document.querySelectorAll.pprices; priceDivs.forEachfunction priceDiv{ priceDiv.textContent=+priceDiv.textContent.toLocaleString'en-US'; }; 125135135 1361436136 136136136136 ID应该是唯一的。改用类。 querySelector返回第一个匹配元素。改用querySelectorAll。 querySelectorAll返回一个NodeList对象,因此使用forEach方法在列表中循环。 const priceDiv=document.queryselectoral.pprices'; priceDiv.foreach=>{ e、 textContent=+e.textContent.toLocaleString'en-US'; } 125135135 1361436136
136136不能有多个唯一标识符,即ID.document.querySelector,因为ID应该是唯一的,所以只能返回第一个元素!