Ajax append load

2019-03-16 05:48发布

it must be jquery

I have file text.html with 6 div in (a,b,c,d,e,f)

In another file i have a div, i like it to populate the content of a+b+c+d+e+f into that single div

I have try .load = but b remplace a i have try append, but i need a temp var

so now i am stuck

That code get the content from the file textes.html ... div #a and put the content into div #right, but the second libe REMPLACE the content of right a with right b

I like to append the content a + b NOT a over b

$(document).ready(function(){
var temp = load('textes.html #nicolas');
$('#right').append(temp);
var temp = load('textes.html #antoine');
$('#right').append(temp);
.
.
.
.

return false;
});

that code is the idea behind what should work, but i cannot make a ajax .load() to load content into a variable to append the content to the div...

<script type="text/javascript">
$(document).ready(function(){    
$.ajax({
  url: "textes.html",
  cache: false,
  success: function(html){
    $("#right").append(html);
  }
});
});
</script>

That code load the WHOLE html file, i like to get only some selected DIV #

标签: ajax load append
5条回答
Emotional °昔
2楼-- · 2019-03-16 06:31
$(document).ready(function(){    
    $.get("textes.html",function(data){
        $("#right").append($("#nicolas",data)).end().append($("#antoine",data));
    },'html');    
});
查看更多
混吃等死
3楼-- · 2019-03-16 06:32

try,

$.get('url.php', function(data) {
    $("#right").append(data);
});
查看更多
劳资没心,怎么记你
4楼-- · 2019-03-16 06:32

JQuery ( http://jquery.com/ ) is a good javascript library that you can use to do an AJAX request to get the other file. See this question for more: Use jQuery to replace XMLHttpRequest

查看更多
手持菜刀,她持情操
5楼-- · 2019-03-16 06:33

This sounds like jQuery? Please state what framework you are using since I can't really see any mention of it. Anyway, append should work. Just do something like:

mydiv.append(a.text());
mydiv.append(b.text());
mydiv.append(c.text());
mydiv.append(d.text());
mydiv.append(e.text());
mydiv.append(f.text());

They should all be appended into mydiv. NOTE: if you also want the html, use the .html() function instead of .text().

查看更多
成全新的幸福
6楼-- · 2019-03-16 06:37

I had a similar issue just now and I think I figured out a way to do what we want using the .load() function. It's not pretty but never mind ;)

First off, I added a "TempDiv" to my html with a "visibility:hidden" style.

<div id="TempDiv" style="visibility:hidden"></div>

Then you run the jQuery :

$(document).ready(function(){
        $('#TempDiv').load('textes.html #nicolas', function(){
            $('#right').append($('#TempDiv').html());
        });

    });

I'm not sure it's the best way !

PS : That is my first stackoverflow post ;)

查看更多
登录 后发表回答