2

我使用iframe在我的 CI 项目中添加了一个 html 页面。现在我想从包含的页面中删除第一个 div 。该div没有ID。我写了一个 jquery 代码,但它删除了整个 iframe。这里是

$(document).ready(function(){
  $("iframe").load(function(){
    //$('div:first').remove(); 
      $("body").find("div:first-child").remove();   
  });
}); 

html

<div class="span12">
                <iframe id="patrolIframe" width="100%" height="900" frameborder="0" scrolling="yes" allowtransparency="true" 
                src="http://...../map.html"></iframe>
            </div>
4

3 回答 3

2
$(document).ready(function(){
  $("iframe").load(function(){
      $('div')[0].remove(); 
     });
}); 
于 2013-04-16T10:55:18.867 回答
0

尝试

$(document).ready(function(){
  $("iframe").load(function(){
    //$('div:first').remove(); 
      $(this).find("div:first-child").remove();   
  });
});

您的 $("body") 选择器选择您的 jquery 加载到的页面的正文,即整个页面。我猜你的 iframe 在那个页面的第一个 div 里面,所以它被删除了。我不使用 iframe,但我认为这种情况下的 $(this) 是指 iframe,所以它应该可以工作。

于 2013-04-16T10:54:46.753 回答
0

尝试

 $(document).ready(function(){
   $("iframe").load(function(){
   $("body",this).find("div:first-child").remove();   
  });
}); 
于 2013-04-16T10:56:22.540 回答