的 onchange 事件当我无法控制元素何时更改时
我的页面上有一个 div 元素,当某些事件发生时,该元素会添加其他 div。我不知道这些事件何时发生、如何触发以及如何将其他 div 添加到主 div(它是外部的,所以我不知道发生了什么,我只能访问该 div)。
我如何“监视”主 div 的更改,以便在添加新 div 时触发事件?我知道我可以使用 setInterval (这就是我现在正在做的),但我真的不想使用它,除非绝对必要。我喜欢像表单元素一样简单的 onchange 功能,但我还没有找到 div 的类似功能。
如果文本不清楚,则示例:
wesite.com/index.html(我唯一可以访问的内容)
<html>
<head></head>
<body>
<div id="theDiv">
</div>
</body>
</html>
,有时我不知道 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>
假设我将事件设置为触发警报,在这个例子中,我希望每次添加新的 div 时都看到一个警报框,所以当它看起来像这样时,我应该已经看到 5 个警报框。
抱歉,如果这很难理解..
I have a div element on a page that has other divs added to it when certain events happen. 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).
How would I "monitor" for changes to the main div so an event is triggered whenever a new div is added ? 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. I would love to something as simple as form elements do with onchange but I have not found such for divs.
Example if text is not clear:
wesite.com/index.html ( the only thing I have access to)
<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 :
<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.
Sorry if this is difficult to follow..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是拦截将子项添加到 DIV 的调用。这不是一个选择吗?
The easiest way is to intercept the calls that are adding the children to the DIVs. Is this not an option?