search

Home  >  Q&A  >  body text

javascript - How to listen for page element additions

When dynamically adding elements to the page, how can we trigger an event by judging the length change of the target element?

大家讲道理大家讲道理2777 days ago994

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-07-05 10:50:31

    Listen to this event DOMSubtreeModified.
    means that if the child node of the currently monitored element is changed: including deleting a child node, adding a child node, or modifying the content of a child node, this event will be triggered.

    var container = document.querySelector(".target")
    container.addEventListener('DOMSubtreeModified', function () {
        // do sth
    }, false);

    reply
    0
  • 大家讲道理

    大家讲道理2017-07-05 10:50:31

    You add a listening event in the dynamically adding element button, and then when the listening event is triggered, judge the length of the target element, and if there is a change, trigger an event

    reply
    0
  • 大家讲道理

    大家讲道理2017-07-05 10:50:31

    Can you determine the length of the target element to trigger it after you add the element dynamically?

    reply
    0
  • Cancelreply