简体   繁体   English

jQuery-从外部页面加载不同的div

[英]jquery - load different divs from a external page

I have this code and I want load from page load.php the text for each div in this page only with a load request from page load.php . 我有这段代码,我只想从页面load.php加载此页面中每个div的文本,而只有页面load.php的加载请求

The example below load the page load.php twice :( 下面的例子两次加载页面load.php :(

$('#div1').load('load.php #div1');
$('#div2').load('load.php #div2');

<div id="div1"></div>
<div id="div2"></div>

load.php page I have the code code: load.php页面上有以下代码:

<div id="div1">test to load1</div>
<div id="div2">test to load2</div>

How can I do it? 我该怎么做?

You could load the php using jQuery.get and then insert each fragment in to the desired elements: 您可以使用jQuery.get加载php,然后将每个片段插入所需的元素中:

$.get({
    url: 'page.php',
    dataType: 'html',
    success: function(response) {
        var html = $($.parseHTML(response));
        $('#div1').text(html.filter('#div1').text());
        $('#div2').text(html.filter('#div2').text());
    }
});

Edit/note if the elements ( #div1 and #div2 ) are at the top level of the loaded html, use: html.filter('#div1') ; 编辑/注释元素( #div1#div2 )是否位于已加载的html的顶级,请使用: html.filter('#div1') ; otherwise use html.find('#div1') to select the desired node. 否则,请使用html.find('#div1')选择所需的节点。

More about jQuery.get . 有关jQuery.get的更多信息。

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

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