简体   繁体   English

onchange 事件<div>当我无法控制元素何时更改时

[英]onchange event for <div> element when I have no control over when it is changed

I have a div element on a page that has other divs added to it when certain events happen.我在页面上有一个 div 元素,当某些事件发生时,该元素添加了其他 div。 I do not know when these events happen, how they are triggered and how the other divs are added to the main div (its external so I have no idea whats happening all I have access to is the div).我不知道这些事件何时发生,它们是如何触发的,以及其他 div 如何添加到主 div(它是外部的,所以我不知道发生了什么,我只能访问 div)。

How would I "monitor" for changes to the main div so an event is triggered whenever a new div is added ?我将如何“监视”主 div 的更改,以便在添加新 div 时触发事件? I know I can use setInterval (which is what I'm doing now), but I really do not want to use it unless absoluley necessary.我知道我可以使用 setInterval (这就是我现在正在做的),但我真的不想使用它,除非绝对必要。 I would love to something as simple as form elements do with onchange but I have not found such for divs.我很想使用 onchange 做一些像表单元素一样简单的事情,但我还没有为 div 找到这样的东西。

Example if text is not clear:文本不清晰的示例:

wesite.com/index.html ( the only thing I have access to) wesite.com/index.html(我唯一可以访问的)

<html>
<head></head>
<body>
    <div id="theDiv">
    </div>
</body>
</html>

and at times unknown to me divs get added to that div and it eventually looks like :有时我不知道 div 会被添加到该 div 中,它最终看起来像:

<html>
<head></head>
<body>
    <div id="theDiv">
        <div class="added"><p>Added Div</p></div>
        <div class="added"><p>Added Div</p></div>
        <div class="added"><p>Added Div</p></div>
        <div class="added"><p>Added Div</p></div>
        <div class="added"><p>Added Div</p></div>
    </div>
</body>
</html>

And lets say I set up the event to trigger an alert, in this example I want to see an alert box every time a new div is added so by the time it looks like this I should have seen 5 alert boxes.假设我设置了触发警报的事件,在这个例子中,我想在每次添加新 div 时看到一个警报框,所以当它看起来像这样的时候,我应该看到 5 个警报框。

Sorry if this is difficult to follow..对不起,如果这很难遵循..

The easiest way is to intercept the calls that are adding the children to the DIVs.最简单的方法是拦截将子项添加到 DIV 的调用。 Is this not an option?这不是一个选择吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM