JQuery - ajax load

旧时模样 提交于 2020-01-14 06:11:52

问题


Some content...

Now I need to load some file into inner-block, but save it's old content.

$('#inner').load( 'pathToFile.html' );

Will replace old content of div.

Thanks.


So, as I understand my code should be:

old = $('#inner').html();
$('#inner').load( 'pathToFile.html' );
$('#inner').html( old + $('#inner').html() );

?


回答1:


I'd recommend against using stuff like 'load' and the other ajax helpers. They're just wrappers around $.ajax. Off the top of my head, maybe you want:

$.ajax( {
  url: 'pathToFile.html',
  type: 'get',
  success: function( r ) {
    $('#inner').append( r );
  }
} );



回答2:


Instead of the way you doing you need to look at these functions ..

append( content )   Returns: jQuery

Append content to the inside of every matched element.

appendTo( selector )    Returns: jQuery

Append all of the matched elements to another, specified, set of elements. As of jQuery 1.3.2, returns all of the inserted elements.

prepend( content )  Returns: jQuery

Prepend content to the inside of every matched element.

prependTo( selector )   Returns: jQuery

and yes @thenduks method is a better way.




回答3:


var oldHtml = $('#inner').html();
$('#inner').load( 'pathToFile.html' );



回答4:


var originalContent=$('#inner').html();
$('#inner').load( 'pathToFile.html' );

is that what you are after?



来源:https://stackoverflow.com/questions/1544542/jquery-ajax-load

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!