收听页面内容已更新
由于现在ajax被广泛使用,很多页面内容都是异步加载的。有没有办法知道所有 DOM 加载完成后是否加载了某些内容?例如,整个页面已加载,但某些图像是通过new image()
创建/加载的,我如何知道Javascript在网页中发生了这些类型的变化?任何活动都可能有用吗?
Since ajax is used widely today, many page contents are loaded asynchronously. Is there any way to know something is loaded after all DOMs are loaded? For example, whole page is loaded, but some images are created/loaded by new image()
, how can I know these kinds of change happened in the web page by Javascript? Any event could be useful?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
已经提到的突变事件应该可以解决问题。如果使用 jQuery,您可以使用“DOMSubtreeModified”突变事件执行类似的操作:
任何内容更改都将显示警报框。在该示例中,当单击 id 为“some-button”的按钮时,内容将添加到正文并显示警报。突变事件提供了一些更具体的事件类型,我所展示的事件可能是最一般的。
The mutation events as already mentioned should do the trick. If using jQuery, you can do something like this with the 'DOMSubtreeModified' mutation event:
Any content changes will display the alert box. In that example, when a button with id "some-button" is clicked, content is added to the body and the alert is shown. The mutation events offer some more specific type of events, the one I have shown is probably the most general.
有一些突变事件: http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-MutationEvent
onsubtreemodified
onnodeinserted
onnoderemoved
等等
看这里:https://developer.mozilla.org/en/DOM_Events
There are some mutation events: http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-MutationEvent
onsubtreemodified
onnodeinserted
onnoderemoved
etc.
Look at here: https://developer.mozilla.org/en/DOM_Events
以下是可能有帮助的代码段:-
您必须更改图像源。事件
onreadystatechange
将帮助您查明图像是否已加载。如果我在获取页面时没有弄错的话,readyState
必须是 AJAX 发送的complete
或4
。或者您也可以使用
上述代码:http: //www.techrepublic.com/article/preloading-and-the-javascript-image-object/5214317
在此代码段中,我们使用
onload
事件来触发图像加载希望如此有帮助。如果这有帮助,请将其标记为答案! :D
干杯
Following is a code piece which may help:-
You must change the image source. The event
onreadystatechange
will help you to find out if the image has been loaded or not.readyState
must becomplete
or4
as sent by AJAX if I'm not mistaken when the page is fetched.OR YOU MAY GO WITH
CREDIT FOR THE ABOVE CODE: http://www.techrepublic.com/article/preloading-and-the-javascript-image-object/5214317
In this code piece, we use
onload
event to trigger the image loadHope this helps. Mark this as the answer if this helps! :D
Cheers