contenteditable change events

前端 未结 19 2354
粉色の甜心
粉色の甜心 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 01:43

    Based on @balupton's answer:

    $(document).on('focus', '[contenteditable]', e => {
    	const self = $(e.target)
    	self.data('before', self.html())
    })
    $(document).on('blur', '[contenteditable]', e => {
    	const self = $(e.target)
    	if (self.data('before') !== self.html()) {
    	  self.trigger('change')
    	}
    })

提交回复
热议问题