![](/img/trans.png)
[英]Calling a function inside document.ready from outside 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.