在iframe中运行的javascript会影响主页吗?

ian*_*ian 5 javascript iframe

部分代码:

我的下面的代码从我的数据库中提取查询,然后使用inner.HTML =来显示div中的数据.它在原始使用中工作正常....

但是,以下版本在iFrame中调用,因为它用于更新页面.

页面没有错误,然后JavaScript触发,但最后一行不起作用...

我刚刚意识到,可能因为它是在隐藏的iFrame中加载它试图在iFrame中设置div的innerHTML,当然这不会起作用.

这是发生了什么?它没有意义,因为我有另一个脚本以相同的方式在它的末尾调用JavaScript,它工作正常.

<?php
    while ($row = mysql_fetch_array($result))
    {
        $p = $p.'<div id="left"><img src="http://www.sharingizcaring.com/bleepV2/images/thumbs/'.$row[image].'" /></div>';
        $p = $p.'<div id="right"><h2>'.$row[artist].' - '.$row['title'].'</h2><br>'.$row['message'].'<br>';
        $p = $p.'<a href="http://www.sharingizcaring.com/bleepV2/'.$row[username].'">'.$row[username].'</a> '.$row[date].'</div>';
        $p = $p.'<div style="clear: both;"></div>';
        $p = $p.'<div id="dotted-line"></div>';
    }


    $p = addslashes($p);
?>

<script>

        alert('posts are firing? ');

        document.getElementById('posts').innerHTML = 'why doth this faileth?';
</script>
Run Code Online (Sandbox Code Playgroud)

Jia*_*aro 6

你能行的!在这里阅读更多信息

您可以通过设置和获取window元素中的变量来影响包含iframe的文档:

// normally you use...
var whatever = "value";

// use the window object instead, which is shared 
// between the iframe and the parent
window.whatever = "value";
Run Code Online (Sandbox Code Playgroud)

您应该知道的另一件事是您可以通过父对象访问主文档

在iframe中你可以使用...

parent.someattr;

// or try this
parent.getElementById('some_element');
Run Code Online (Sandbox Code Playgroud)