繁体   English   中英

从另一个调用文件的document.ready函数

[英]Calling document.ready function of file from another

我有两个HTML文件。 一个文件有一个表,该表由用同一脚本编写的脚本不断更新。 该表部分位于html的正文中,并且一旦执行document.ready,其内容就会更新。 我想运行另一个文件中的document.ready文件,并在该文件中显示表(该表不断更新并存在于另一个文件中)。

文件-1

<script type="text/javascript">
   $(document).ready(function () {

      $('#maindiv').load('http://somesite/index.html #tableId');


       });
 </script>
<body>
<div id="maindiv"></div>
</body>

FILE-2(index.html页面)

<script>

      $(document).ready(function(){

          $.ajaxSetup({ cache: false });
          some_function(true);
          setInterval(function(){some_function(false)},500);
      });

      //Some Global Variables Declared

var some_function= function(getAll){
//Accessing and using the above variables
}
</script>
<body>
<div id="tableId">
<table>
 ......
</table>
</div>
</body>

如果我说对了。 您应该执行全局功能 假设您有以下情况。

档案一

$(document).ready(function () {
      doSomething();     
});

function doSomething() {
    //doing something
}

档案二

$(document).ready(function() {
    doSomething();
});



编辑
如果要从另一个页面获取html,则可以使用jQuery .load() 您将需要具有使它在“导入”到文件上动态的JavaScript。

文件一个html

<div class="load-table-here"></div>

文件两个html

<table class="you-want-me"></table>

你这样加载

UPDATE

$('.load-table-here').load('path/file-two.html .you-want-me', function() {
      some_fuction(true);
      setInterval(function(){some_function(false)},500);
});

在jQuery.load文档中,查看“脚本执行”部分: http ://api.jquery.com/load/。 这里要指出的重要例子是,

但是,在以下情况下,将文档中正在加载到#b中的脚本块删除并且不执行:

 $( "#b" ).load( "article.html #target" ); 

因此,换句话说,永远不会执行为您的其他文件加载的脚本,也永远不会更新您的新表。

暂无
暂无

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

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