JavaScript-How to monitor the content of an element is modified this event?

Source: Internet
Author: User
For example, I have a P element, inside is some text, and then I have a button, click the button to replace the contents of the P element, then the P element binding what event can let it tell me that it has been modified, what is the implementation method?

Reply content:

For example, I have a P element, inside is some text, and then I have a button, click the button to replace the contents of the P element, then the P element binding what event can let it tell me that it has been modified, what is the implementation method?

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

The newest mutationobserver should be able to meet the needs, see Https://developer.mozilla.org/zh-CN/docs/Web/API/MutationObserver

Mutationobserver provides developers with the ability to respond appropriately when a DOM tree changes in a range.

Covers child node changes, attribute changes.

If you use event listeners, such as domnodeinserted, domnoderemoved, and so on are more intuitive. But Mutationobserver was designed to replace Mutation events-domnodeinserted and so on.

This design is a bit like Promise, without the shadow of the event listener.

Thank you for inviting me. Click on the event where you send a ajax request to send your modified action to someone else.

P element itself has no change events, so you have to click on the button to do logic processing, anyway, click on this button will modify the P element content, the bad thing is that if more than one place is the modification of P, this time will be repeated to write the same logic processing, but if you use the framework, For example, angular and Vue offer such watcher.

DOMSubtreeModified
This event is seldom used by people ....

  • 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.