Ajax append load

2019-03-16 06:43发布

问题:

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 #

回答1:

$(document).ready(function(){    
    $.get("textes.html",function(data){
        $("#right").append($("#nicolas",data)).end().append($("#antoine",data));
    },'html');    
});


回答2:

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 ;)



回答3:

try,

$.get('url.php', function(data) {
    $("#right").append(data);
});


回答4:

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().



回答5:

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



标签: ajax load append