[英]Document ready function not working in nested html page : jQuery
I have a HTML page which having 1 div. 我有一个有1个div的HTML页面。
<div id="secondPage"></div>
and having 并拥有
<script>
$(document).ready(function(){
$("secondPage").load("mySecondHtmlPage.html")
})
</script>
This mySecondHtmlPage.hmtl is loading but I want to have another document ready function in that second html page, which is not firing. 这个mySecondHtmlPage.hmtl正在加载,但我希望在第二个html页面中有另一个文档就绪函数,它不会触发。
When I have a jQuery reference in that page(second html) too documentReady function is getting fired but it is not loading properly inside the div. 当我在该页面中有一个jQuery引用(第二个html)时,documentReady函数也被触发但它没有在div中正确加载。
Second html page: 第二个html页面:
<div>
My Content goes here
</div>
<script>
$(document).ready(function(){
alert(''); //Not firing
})
</script>
When I have a jQuery ref over top that alert is firing but it is not getting loaded in 1st html page. 当我有一个jQuery引用顶部时警报正在触发,但它没有在第一个html页面加载。
You can trigger a different event once the page is loaded: 加载页面后,您可以触发不同的事件:
$( "#secondPage" ).load( "mySecondHtmlPage.html", function() {
$(document).trigger('page-ready');
});
Then use this on the second page: 然后在第二页上使用它:
<div>
My Content goes here
</div>
<script>
$(document).on('page-ready', function(){
alert(''); //Not firing
});
</script>
you should use iframe or XMLHttpRequest 你应该使用iframe或XMLHttpRequest
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
// Action to be performed when the document is read;
}
};
xhttp.open("GET", "filename", true);
xhttp.send();
在第一页中放置mySecondHtmlPage.html
中的所有javascript代码,或者更好地从链接到第一页的体验javascript文件中的两个页面中分离javascript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.