【问题标题】:Cant delete elements inside a iframe with javascript written in php无法使用用 php 编写的 javascript 删除 iframe 中的元素
【发布时间】:2018-06-06 17:34:33
【问题描述】:

尝试删除 iframe 内的 3 个元素。负责删除元素体 onload 上调用的元素的“Delete”函数不运行。

 <html>
    <script>
        <?php 
    echo"function delete(){ 
    var elem0=document.getElementById('".$_GET['id']."').contentWindow.document.getElementById('".$id."');
    var elem1=document.getElementById('".$_GET['id']."').contentWindow.document.getElementById('".$id1."');
    var elem2=document.getElementById('".$_GET['id']."').contentWindow.document.getElementById('".$id2."');

    elem0.removeChild(elem0.childNodes[0]);
    elem1.removeChild(elem1.childNodes[0]);
    elem2.removeChild(elem2.childNodes[0]);
    }"; ?>
    </script> 

    <body onload="delete();">
        <div>
        <?php

    $page=$_GET['id'].".html";
    $id=down;
    $id1=gerar1;
    $id2=gerar; 

              echo '<iframe src="' .$page.'" id="'.$_GET['id'].'" frameBorder="0" width="70%" height="100%" align="left" scrolling="no" />';
              echo '</iframe>';
        ?>
    </div>

    <div>
    <?php
    echo'<iframe src="../../galeria/frame2_galeria.html" frameBorder="0" width="29%" height="600px" align="right" scrolling="yes" />    
    </iframe>';
        ?>
    </div>
    </body>

    </html>

【问题讨论】:

  • 这对您有帮助吗? stackoverflow.com/questions/1452871/…
  • 在函数名function delete()之后添加bracket ()
  • @Terry Wei 我的问题是调用该函数,因为当我在控制台“getElement”中运行时,我可以访问它。
  • 如果这一切都能奏效,那么只有将删除内容移到 iframe 的 onload 中才能奏效。
  • @Dr.Strange 我添加了括号,它也不起作用

标签: javascript php html iframe


【解决方案1】:

delete() 是一个关键字,这就是您的函数未被调用的原因

使用不同的函数名代替delete

所以它的作品......!

【讨论】:

  • 还有一件事,当我删除元素时,应用于该元素的 css 保留在页面中,我如何删除它? Bc 应用于按钮的功能保持不变。
【解决方案2】:

我认为问题在于 iframe 没有加载完成。

您只需将 onload 事件处理程序添加到 iframe 或类似 setTimeout 的内容并将 delete 放入其中:

iframe.onload = function() { delete(); };

应该可以的。

【讨论】:

    猜你喜欢
    • 2018-11-14
    • 2020-03-22
    • 2015-10-26
    • 2011-09-20
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    相关资源
    最近更新 更多