简体   繁体   English

jQuery:将其他页面的div加载到div中

[英]jquery: load div from other pages into div

I'd like to load divs that are saved as separate html files into a div of a "main file". 我想将保存为单独的html文件的div加载到“主文件”的div中。 This works fine. 这很好。 However, in the div that I am loading into the main file, I have some links again and when I click on those, I'd like those divs to be loaded into the div in the main file. 但是,在要加载到主文件中的div中,我又有一些链接,当我单击这些链接时,我希望将这些div加载到主文件中的div中。

So the main file has a menu like this: 因此,主文件具有如下菜单:

<ul id="menu_ul">
                <li class=" ui-widget-header">
                    <a class="clickable" href="output/d1e49.html">ONE</a>
                </li>
                <li class=" ui-widget-header">
                    <a class="clickable" href="output/d1e670.html">TWO</a>
                        </li>
</ul>

and a div like this: 和这样的div:

<div id="target"></div>

The jquery looks like this: jQuery看起来像这样:

                    $("a").click(function() {  return false;    }); 

                    $(".clickable").click(function() {   

                    var url = $(this).attr("href");     

                    $('#target').load(url) + " .content";

                    });

And the loaded pages look like this: 加载的页面如下所示:

<div class="content">
                    <ul>
                        <li>
                            <a class="clickable" href="output/d1e100367.html">SUB_ONE</a>
                        </li>
                        <li>
                            <a class="clickable" href="output/d1e101804.html">SUB_TWO</a>
                        </li>
</ul>
</div>

So the pages ONE and TWO are loaded into the div. 因此,页面ONE和TWO被加载到div中。 However, when I click on the links contained in those files, it doesnt work, the links for SUB_ONE and SUB_TWO replace the current page instead of being loaded into the div as well. 但是,当我单击这些文件中包含的链接时,它不起作用,SUB_ONE和SUB_TWO的链接替换了当前页面,而不是也加载到了div中。

How would I get the links in the loaded pages to work? 如何使已加载页面中的链接正常工作? Is the DOM of the loaded divs not accessible to jquery? jQuery无法访问已加载的div的DOM?

This is wrong syntax $('#target').load(url) + " .content"; 这是错误的语法$('#target').load(url) + " .content"; but i guess just typo in question. 但我想只是拼写问题。 It must be: $('#target').load(url + " .content"); 它必须是: $('#target').load(url + " .content");

Now regarding your issue, you should delegate event: 现在关于您的问题,您应该委派事件:

$(document).on("click", ".clickable", function (e) {

  e.preventDefault();

  var url = $(this).attr("href");

  $('#target').load(url + " .content");

});

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

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