取下JQuery的AJAX加载HTML元素()响应 [英] Remove HTML Elements from JQuery AJAX load() Response

查看:84
本文介绍了取下JQuery的AJAX加载HTML元素()响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能将到容器之前删除从AJAX(负载)响应特定的HTML元素?在这种情况下,我想删除#containerTop,包括内容,从响应。

Is there a possibility to remove specific HTML Elements from a AJAX (load) Response before placing in to the container? In this case I want to remove the "#containerTop", including content, from the response.

响应(HTML):

<html>
  <head></head>
  <body>
    <div id="containerMain">
      <div>...</div>
      <div id="containerTop">Content-to-remove-including-container-div...</div>
    </div>
  </body>
</html>

我想这一点,但没有成功。

I've tried this, without success.

<div id="middle"></div>
<script>
$( "#middle" ).load( "http://<URL>",
function(response, status, xhr){
$response.remove('#containerTop');
            });
</script>

任何想法?

推荐答案

.load()直接插入的内容为您服务。它不会给你一个机会修改它插入之前。因此,你有两个选择:

.load() inserts the content directly for you. It does not give you an opportunity to modify it before it is inserted. As such you have two options:

  1. 您可以修改的内容插入之后,但在此之前它被涂在 .load()完成处理。
  2. 您可以切换到获得()来获取内容数据,然后把它变成一个jQuery对象,然后使用jQuery方法进行修改,然后将修改内容到自己的页面。
  1. You can modify the content after it is inserted, but before it is painted in the .load() completion handler.
  2. You can switch to .get() to get the content as data, then put it into a jQuery object, then modify it using jQuery methods, then insert the modified content into your page yourself.

下面是第二个选项的一个例子:

Here's an example of the second option:

$.get("http://<URL>", function(data) {
    var temp = $(data);
    temp.find('#containerTop').remove();
    $('#middle').empty().append(temp);
});

这篇关于取下JQuery的AJAX加载HTML元素()响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆