【问题标题】:how to remove first div from a html page include by iframe using jquery如何使用jquery从iframe中删除html页面中的第一个div
【发布时间】:2013-04-16 10:51:37
【问题描述】:

我使用 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>

【问题讨论】:

    标签: php javascript jquery codeigniter


    【解决方案1】:
    $(document).ready(function(){
      $("iframe").load(function(){
          $('div')[0].remove(); 
         });
    }); 
    

    【讨论】:

    • DOM api 有没有remove() 方法?
    • 我知道jquery方法。我不确定 [0] 返回的是 jQuery 对象还是普通的 dom 元素。
    • @moddasir 你的 div 在 iframe 或 html 里面...??
    • @Prasath 我已经提到过。我想更改 iFrame 中的元素。我从不同的域加载一个 html 页面,我想删除它的第一个 div。
    • 使用 this.$('div')[0].remove();
    【解决方案2】:

    试试

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

    【讨论】:

      【解决方案3】:

      试试

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-24
        • 2019-08-03
        • 2014-11-20
        • 1970-01-01
        相关资源
        最近更新 更多