简体   繁体   English

页面动态加载内容时如何运行javascript函数

[英]How to run a javascript function when a page dynamically loads content

I am currently writing some Javascript code that adds some tags across text in an HTML file after the page loads. 我目前正在编写一些Javascript代码,以在页面加载后跨HTML文件中的文本添加一些标签。 I use the 我用

window.onload

method for achieving this. 实现此目的的方法。

However, I am facing an issue in pages like google plus, where you get more content as you scroll down. 但是,我在google plus之类的页面中遇到问题,向下滚动时您会获得更多内容。 Is there a way of calling my JS function when such a page adds more content? 当此类页面添加更多内容时,是否可以调用我的JS函数?

Thanks 谢谢

Akshay 阿克沙伊

There are several ways how to get this working. 有几种方法可以使它起作用。 You can either use jquery like this: 您可以像这样使用jquery:

$('#mydiv').bind("DOMSubtreeModified",function(){
    // your code goes here
    alert('changed');
});

Note that this is not supported in IE8( and below). 请注意,IE8(及以下)不支持此功能。

Or you can run loop and continuously fire the desired code: 或者,您可以运行循环并连续触发所需的代码:

var interval = setInterval(function() {
    // your code goes here
}, 1000);

Fiddle: http://jsfiddle.net/8RF5r/5/ 小提琴: http : //jsfiddle.net/8RF5r/5/

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

相关问题 页面动态加载Chrome扩展程序内容时如何运行脚本 - How to run script when page loads content dynamically for Chrome Extension jQuery Mobile页面加载时运行Javascript函数 - Run Javascript function when jquery mobile page loads 加载iframe时,如何使iframe在主页上运行Javascript函数? - How do you make an iframe run a Javascript function on the main page when the iframe loads? 页面加载后,如何在Chrome中运行JavaScript代码段? - How can I run JavaScript snippets in Chrome when the page loads? 页面加载时运行javascript函数 - run javascript functions when the page loads 如何使用Jquery在Javascript中加载页面时调用函数 - How to call a function when a page loads in Javascript with Jquery 页面加载时自动启动 JavaScript function - Automaticaly start JavaScript function when page loads 页面加载时需要从单选列表中获取价值并运行javascript函数 - Need to get value from radiolist and run javascript function when page loads 如何在页面加载时停止javascript中的警报触发,并在单击按钮时使其运行? - How can I stop an alert in javascript from firing when the page loads, and make it run when a button is clicked? 如何在javascript中加载html5时停止一个函数并设置其他函数运行 - How to stop a function and set other function to run when it loads in javascript for html5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM