Javascript 为什么这个物体是不可接受的?

Javascript 为什么这个物体是不可接受的?,javascript,html,iframe,Javascript,Html,Iframe,我正在尝试从iframe(交叉原点)中滚动父对象。我找到了(由David j.Bradshaw编写)应该能够实现这一点(我正在使用resizer,因为我还需要iframe根据其内容调整大小) 我可以使用,但因为我已经在使用iframe大小调整器,它有实现我的目标的选项,所以最好充分利用它 通过在父级上使用此选项: iFrameResize({ enablePublicMethods : true }); 我应该能够在iframe中调用window.parentIFrame.sc

我正在尝试从iframe(交叉原点)中滚动父对象。我找到了(由David j.Bradshaw编写)应该能够实现这一点(我正在使用resizer,因为我还需要iframe根据其内容调整大小)

我可以使用,但因为我已经在使用iframe大小调整器,它有实现我的目标的选项,所以最好充分利用它

通过在父级上使用此选项:

iFrameResize({
    enablePublicMethods     : true
});

我应该能够在iframe中调用
window.parentIFrame.scrollTo(0,0)
。但是,由于某些原因,我无法访问
window.parentIFrame

当我在控制台中记录
window
时,我do查看
parentIFrame
对象,但是当我记录
window.parentIFrame
时,它返回
未定义的

为什么我不能访问
窗口。parentIFrame
对象

这是我当前的测试情况(从中复制),可以找到库:

index.html

<!DOCTYPE html> 
<html> 
    <head> 
        <meta charset="utf-8"> 
        <title>iFrame message passing test</title> 
        <meta name="description" content="iFrame message passing test"> 
        <meta name="viewport" content="width=device-width">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <script type="text/javascript">
            //MDN PolyFil for IE8 (This is not needed if you use the jQuery version)
            if (!Array.prototype.forEach){
                Array.prototype.forEach = function(fun /*, thisArg */){
                "use strict";
                if (this === void 0 || this === null || typeof fun !== "function") throw new TypeError();

                var
                t = Object(this),
                len = t.length >>> 0,
                thisArg = arguments.length >= 2 ? arguments[1] : void 0;

                for (var i = 0; i < len; i++)
                if (i in t)
                    fun.call(thisArg, t[i], i, t);
                };
            }
        </script>
    </head> 
    <body> 
        <h2>Automagically resizing iFrame</h2>
        <p>Resize window or click one of the links in the iFrame to watch it resize.</p>
        <div style="margin:20px;">
            <iframe src="frame.content.html" width="100%" scrolling="no"></iframe>
        </div>
        <p id="callback">
        </p>
        <div style="margin: 8px 0;font-size:13px;">
            For details on how this works, see
            <a href="http://davidjbradshaw.github.io/iframe-resizer/">http://davidjbradshaw.github.io/iframe-resizer/</a>.
        </div>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script type="text/javascript" src="iframeResizer.min.js"></script> 
        <script type="text/javascript">

            iFrameResize({
                enablePublicMethods     : true
            });


        </script>

    </body> 
</html>
<!DOCTYPE html> 
<html> 
    <head> 
        <meta charset="utf-8"> 
        <title>iFrame message passing test</title> 
        <meta name="description" content="iFrame message passing test">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <style>
            *, *:before, *:after {box-model: border-box;}
            a { float:right; margin-left:10px;}
        </style>
    </head> 
    <body> 

        <b>iFrame</b> 
        <a href='#' onclick="$('p:eq(0)').toggle();return false;">Toggle content</a>
        &nbsp;
        <a href='#' onclick="if ('parentIFrame' in window) window.parentIFrame.size(250);return false;">Size(250)</a>
        &nbsp;
        <a id="nested" href="frame.nested.html">Nested</a>
        &nbsp;
        <a href="frame.hover.html">:Hover</a>
        &nbsp;
        <a href="frame.absolute.html">Absolute Position</a>
        &nbsp;
        <a href='#' onclick="if ('parentIFrame' in window) window.parentIFrame.sendMessage('Hello from the iFrame');return false;">Send Message</a>
        &nbsp;
        <a href='#' onclick="if ('parentIFrame' in window) window.parentIFrame.close();return false;">Close</a>

        <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </p>
        <p>
            But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?
        </p>
        <p>
            On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.
        </p>

        <script>

            var level = document.location.search.replace(/\?/,'') || 0;

            $('#nested').attr('href','frame.nested.html?'+(++level));
            console.log(window); // Does show parentIFrame in the list of properties/objects
            console.log(window.parentIFrame); // Returns undefined
        </script>


        <script type="text/javascript" src="iframeResizer.contentWindow.min.js"></script> 
    </body>
</html>

iFrame消息通过测试
//MDN PolyFil for IE8(如果使用jQuery版本,则不需要此选项)
if(!Array.prototype.forEach){
Array.prototype.forEach=函数(fun/*,thisArg*/){
“严格使用”;
如果(this==void 0 | | this==null | | | typeof fun!==“函数”)抛出新的TypeError();
变量
t=对象(此),
len=t.length>>>0,
thisArg=arguments.length>=2?参数[1]:无效0;
对于(变量i=0;i

有关其工作原理的详细信息,请参阅 . iFrameResize({ enablePublicMethods:true });
frame.content.html

<!DOCTYPE html> 
<html> 
    <head> 
        <meta charset="utf-8"> 
        <title>iFrame message passing test</title> 
        <meta name="description" content="iFrame message passing test"> 
        <meta name="viewport" content="width=device-width">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <script type="text/javascript">
            //MDN PolyFil for IE8 (This is not needed if you use the jQuery version)
            if (!Array.prototype.forEach){
                Array.prototype.forEach = function(fun /*, thisArg */){
                "use strict";
                if (this === void 0 || this === null || typeof fun !== "function") throw new TypeError();

                var
                t = Object(this),
                len = t.length >>> 0,
                thisArg = arguments.length >= 2 ? arguments[1] : void 0;

                for (var i = 0; i < len; i++)
                if (i in t)
                    fun.call(thisArg, t[i], i, t);
                };
            }
        </script>
    </head> 
    <body> 
        <h2>Automagically resizing iFrame</h2>
        <p>Resize window or click one of the links in the iFrame to watch it resize.</p>
        <div style="margin:20px;">
            <iframe src="frame.content.html" width="100%" scrolling="no"></iframe>
        </div>
        <p id="callback">
        </p>
        <div style="margin: 8px 0;font-size:13px;">
            For details on how this works, see
            <a href="http://davidjbradshaw.github.io/iframe-resizer/">http://davidjbradshaw.github.io/iframe-resizer/</a>.
        </div>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script type="text/javascript" src="iframeResizer.min.js"></script> 
        <script type="text/javascript">

            iFrameResize({
                enablePublicMethods     : true
            });


        </script>

    </body> 
</html>
<!DOCTYPE html> 
<html> 
    <head> 
        <meta charset="utf-8"> 
        <title>iFrame message passing test</title> 
        <meta name="description" content="iFrame message passing test">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <style>
            *, *:before, *:after {box-model: border-box;}
            a { float:right; margin-left:10px;}
        </style>
    </head> 
    <body> 

        <b>iFrame</b> 
        <a href='#' onclick="$('p:eq(0)').toggle();return false;">Toggle content</a>
        &nbsp;
        <a href='#' onclick="if ('parentIFrame' in window) window.parentIFrame.size(250);return false;">Size(250)</a>
        &nbsp;
        <a id="nested" href="frame.nested.html">Nested</a>
        &nbsp;
        <a href="frame.hover.html">:Hover</a>
        &nbsp;
        <a href="frame.absolute.html">Absolute Position</a>
        &nbsp;
        <a href='#' onclick="if ('parentIFrame' in window) window.parentIFrame.sendMessage('Hello from the iFrame');return false;">Send Message</a>
        &nbsp;
        <a href='#' onclick="if ('parentIFrame' in window) window.parentIFrame.close();return false;">Close</a>

        <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </p>
        <p>
            But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?
        </p>
        <p>
            On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.
        </p>

        <script>

            var level = document.location.search.replace(/\?/,'') || 0;

            $('#nested').attr('href','frame.nested.html?'+(++level));
            console.log(window); // Does show parentIFrame in the list of properties/objects
            console.log(window.parentIFrame); // Returns undefined
        </script>


        <script type="text/javascript" src="iframeResizer.contentWindow.min.js"></script> 
    </body>
</html>

iFrame消息通过测试
*,*:前,*:后{box model:border box;}
a{浮点:右;边距左:10px;}
iFrame

知识本身是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德有时,如果你不自以为是,就必须为自己的行为负责。

但我必须向你们解释,谴责快乐和赞美痛苦的错误观念是如何产生的,我将给你们一个完整的系统描述,并阐述伟大的真理探索者、人类幸福的建设者的实际教导。没有人拒绝、不喜欢或回避快乐本身,因为快乐本身就是快乐,但它是幸福因为那些不知道如何理性地追求快乐的人会遇到极度痛苦的后果。也没有人爱、追求或渴望获得痛苦本身,因为痛苦本身就是痛苦,但因为偶尔会发生一些情况,在这些情况下,辛劳和痛苦可以使他获得一些巨大的快乐我们当中有谁曾经进行过艰苦的体育锻炼,只是为了从中获得一些好处?但是,谁有权利对一个选择享受没有烦人后果的快乐的人,或者一个选择避免没有结果快乐的痛苦的人,挑毛病呢?

另一方面,我们义愤填膺地谴责和厌恶那些被当下快乐的魅力所迷惑、意志消沉、被欲望所蒙蔽、无法预见必然会发生的痛苦和麻烦的人;同样的责任属于那些由于意志薄弱而失职的人,这与萨科齐一样在一个自由的时间里,当我们的选择权不受限制,当没有任何事情阻止我们做我们最喜欢做的事情时,每一种快乐都是受欢迎的,每一种痛苦都是可以避免的。但是在某些情况下,由于责任或责任的要求商业的义务经常发生的事情是,快乐必须被拒绝,烦恼必须被接受。因此,智者在这些事情上总是坚持这个选择原则:他拒绝快乐以获得其他更大的快乐,或者他忍受痛苦以避免更大的痛苦。

变量级别=document.location.search.replace(/\?/,'')| 0; $('#nested').attr('href','frame.nested.html?'+(++level)); console.log(window);//是否在属性/对象列表中显示parentIFrame console.log(window.parentIFrame);//返回未定义的
找到了。我不知道为什么,但之所以
窗口。parentIFrame
未定义,是因为加载页面后无法立即使用

因此(使用jQuery):

不起作用。但将其绑定到单击或其他内容:

$(document).ready(function(){
    $('#test').click(function(){
        window.parentIFrame.scrollTo(0,0);
    });
});

确实有效。

事实上,在document.ready之后不能直接使用parentIframe。您可以等待一秒钟或更长时间。因此需要将其设置为如下设置间隔:

$(文档).ready(函数(){