简体   繁体   English

文档就绪函数在嵌套的html页面中不起作用:jQuery

[英]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>

Read more about .trigger() here . 在这里阅读更多关于.trigger() 信息

you should use iframe or XMLHttpRequest 你应该使用iframeXMLHttpRequest

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.

相关问题 JQuery $(document).ready(function()无效 - JQuery $(document).ready(function () not working jQuery(文档).ready(函数(){}); 不能正常工作 - jQuery(document).ready(function(){}); not working properly 我的文档.ready功能上的jQuery块无法正常工作 - Jquery Block on my document .ready function not working 如何在jQuery中声明此函数? $ {document).ready无法正常工作 - How to declare this function in jQuery? $(document).ready is not working Jquery 触发器 function 未准备好文档 - Jquery Trigger function not working with document ready jQuery $(document).ready函数停止工作 - jquery $( document ).ready function stopped working scrollToTop不能处理jQuery(document).on(&#39;ready&#39;,function(){ - scrollToTop is not working on jQuery(document).on('ready', function() { $(document).ready 上的函数不起作用 - function on $(document).ready not working 如果Javascript代码块不在HTML文件的末尾,而是使用jQuery的$(document).ready(function(){...}),它会减慢页面显示吗? - If Javascript code block is not at end of HTML file, but is using jQuery's $(document).ready(function() {…}), will it slow down the page display? 如何使用嵌套在HTML中的$(document).ready()调用函数 - How to invoke a function with a $( document ).ready() nested inside in HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM