contenteditable change events

前端 未结 19 2325
粉色の甜心
粉色の甜心 2020-11-22 01:36

I want to run a function when a user edits the content of a div with contenteditable attribute. What\'s the equivalent of an onchange

19条回答
  •  广开言路
    2020-11-22 02:04

    const p = document.querySelector('p')
    const result = document.querySelector('div')
    const observer = new MutationObserver((mutationRecords) => {
      result.textContent = mutationRecords[0].target.data
      // result.textContent = p.textContent
    })
    observer.observe(p, {
      characterData: true,
      subtree: true,
    })

    abc

提交回复
热议问题