JS原生操作DOM(更新中)

时间:2024-11-19 07:42:46
// 添加查看原图按钮 let grands = document.getElementsByClassName("item-detail"); for(let i=0; i<grands.length; i++){ let oImgs = grands[i].getElementsByTagName("img"); for(let j=0; j<oImgs.length; j++){ let oDiv = document.createElement("div"); let oBtn = document.createElement("input"); oBtn.setAttribute('data-src', oImgs[j].src); oBtn.setAttribute('type','button'); oBtn.setAttribute('class', 'seeBigPic'); oBtn.setAttribute('value', '查看原图'); // 通过img宽度设置按钮left oBtn.setAttribute('style', 'left:' + (oImgs[j].width/2 - 39) + 'px'); var p = oImgs[j].parentNode; p.insertBefore(oDiv, oImgs[j]); oDiv.appendChild(oImgs[j]); oDiv.appendChild(oBtn); oDiv.setAttribute('style', 'position: relative'); } }