Javascript-how to monitor the event where element content is modified?

Source: Internet
Author: User
For example, I have a p element, which contains some text, and then I have a button, click the button to replace the content in the p element, so what kind of events can be bound to element p to notify me that it has been modified? is there any implementation method? For example, I have a p element, which contains some text, and then I have a button, click the button to replace the content in the p element, so what kind of events can be bound to element p to notify me that it has been modified? is there any implementation method?

Reply content:

For example, I have a p element, which contains some text, and then I have a button, click the button to replace the content in the p element, so what kind of events can be bound to element p to notify me that it has been modified? is there any implementation method?

Http://stackoverflow.com/questions/9470931/is-there-any-on-dom-change-event

The latest MutationObserver should be able to meet your needs, see https://developer.mozilla.org/zh-CN/docs/Web/API/MutationObserver

MutationObserver provides developers with the ability to make appropriate responses when DOM trees change within a certain range.

Covers sub-node changes and attribute changes.

If event listeners, such as DOMNodeInserted and DOMNodeRemoved, are used, they are more intuitive. However, MutationObserver is designed to replace Mutation events -- DOMNodeInserted.

This design is somewhat like Promise, with no event listener shadow.

Thank you for your invitation. Click the event to sendajaxRequest to send the modified operation to another user.

The p element does not exist.changeEvent, so you have to perform logic processing when you click the button. clicking this button will definitely modify the content of the p element. The bad thing is that if you modify p in multiple places, in this case, we need to repeat the same logic processing. However, if you use a framework, such as watcher provided by angular and vue

DOMSubtreeModified
This event is rarely used ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.