javascript - jQuery `replaceWith();` Can we replace a div (not load into) with php from external file? - Stack Overflow

Can I replace a <div> with external php script. Something like this:$('#aside').replace

Can I replace a <div> with external php script. Something like this:

$('#aside').replaceWith('blocks/filename.php');

Please be gentle I have just started to learn JavaScript.

UPDATE:

I want to replace that <div id="aside">. I want to remove it pletely and place the new content there.

Can I replace a <div> with external php script. Something like this:

$('#aside').replaceWith('blocks/filename.php');

Please be gentle I have just started to learn JavaScript.

UPDATE:

I want to replace that <div id="aside">. I want to remove it pletely and place the new content there.

Share edited Jul 2, 2013 at 13:15 Vikram Rao asked Jul 2, 2013 at 13:05 Vikram RaoVikram Rao 1,0784 gold badges25 silver badges62 bronze badges 3
  • 1 no, you cannot do that for obvious reasons. What you can do, is to make an ajax call to that php page, and based on the response, replace #aside div. – Twisted1919 Commented Jul 2, 2013 at 13:07
  • 3 Not like that, no, but have a look at the .load ajax method – Bergi Commented Jul 2, 2013 at 13:07
  • Use either .load() to fetch the contents and load them directly into a holder element, or use .ajax() (or .get(), which really is just a shorthand) to fetch the content, then manually put it inside the holder. – qwerty Commented Jul 2, 2013 at 13:11
Add a ment  | 

3 Answers 3

Reset to default 6

You can do this - if you want to replace #aside with new content

$.get("blocks/filename.php", function(data) {
    $('#aside').replaceWith($(data));
});

Not that simply, you can load your PHP into said div tho with a simple .load call:

$("#aside").load("blocks/filename.php", function() {
    console.log("I've been loaded!");
})

API Ref: http://api.jquery./load/

Per the edits, you'll want to use a $.get function with a callback to replace that div with the new content.

You want to load the contents from a PHP-file, and put it inside a <div> right? The very easy way would be to send a AJAX GET-request to the file, and fill the contents as such:

$.ajax({
  url: 'blocks/filename.php', 
  data: {}, 
  success: function(data) {
    $('#aside').replaceWith(data);
  }), 
  dataType: 'html'
}); 

EDIT: Changed to replaceWith() instead, as suggested.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745067517a4609341.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信