1 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以通過創(chuàng)建一個(gè) dom 元素來實(shí)現(xiàn)這一點(diǎn),該元素包含您的 html 并使用 dom api 對其進(jìn)行請求,這比像文本一樣解析它更干凈、更容易
var fullHtml = `<div id="comments-box" contenteditable="true" data-text="Enter comment">
<a class="test-image test-upload" contenteditable="false" fileindex="0"
image-title="great_life5.jpg" href="#" target="_blank">
<span>great_life5.jpg</span></a>
<a class="test-image test-upload" contenteditable="false" fileindex="1"
image-title="greatlife_updated.png" href="#" target="_blank">
<span>greatlife_updated.png</span></a>
</div>`
const container = document.createElement("div")
container.innerHTML = fullHtml;
[1,2].forEach((item, index) => {
let e = container.querySelector(`a[fileindex='${index}']`)
e.setAttribute('yourAttribute', 'yourValue');
e.setAttribute('image-timestamp', new Date().toString())
})
console.log(container.innerHTML)
添加回答
舉報(bào)