使用Javascript:可以将数据通过iframe传递双向?([removed] Can data

2019-08-20 08:35发布

对于它的特定知识的人,这一切都涉及到WordPress的“主题定制”,尽管这没有必要回答这个问题。

基本上,我有一个页面(在PHP),其含有的适用的iframe,这是在屏幕的右部中的内容设置的左侧“窗格”。

从概念上讲,我期待用jQuery的可拖动 ,使CSS background-position的主要背景图像通过拖动功能可调的。 我需要申请draggable()在上customize.php页面,排队的脚本在那里,然后操纵IFRAME里的DOM。 它需要在容器/母公司层面做,因为没有那个水平,这将更新X和Y坐标,并将它们保存在存储在那里设置的设置。

我想我需要的可能是不可能的,但我不知道是否有可能用JavaScript做以下两项操作:

  1. 获取iframe的内容
  2. 操作DOM 的iframe内 ,通过调整背景图像draggable()和存储background-position坐标作为数据属性,然后将它们保存到适当的设定输入字段。

根据我的经验,从“父”操纵DOM的iframe中/容器水平是困难的,我想知道如果有件事情我不知道,这是绝对不可能的,或者有一些解决方法吗? (请注意,原始域是相同的,所以不会有任何跨域iframe的问题)

Answer 1:

下面是我的测试执行父iframe的沟通减少的代码。 我加了一些样品如何可以使用。 但我必须指出,这是我的实验室部分,我目前不不得不检查它是否是完全跨浏览器兼容的时间。 但如果它不只会有一些小的变化是有许多工作要做。 (EDIT在当前铬测试,FF和IE 6-9)

我希望这会帮助你与你的问题。 而我又不能保证,现在,它会很好地工作(但我敢肯定)。

父代码

<!doctype html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>index</title>
    <script src="jquery.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" charset="utf-8">

    (function($, window, undefined) {

        var iframe;

        // the connector object the will be passed to the iframe and will be used as "API"
        var parentConnector = {
            _window   : window, // a reference to the parents window (not really necessary but nice to have)
            $ : $,              // a reference to the parents jQuery that can be accessed from the iframe (use with caution)

            // here you will define you "API" for the parent (the functions you want to call from the iframe )
            setElementValue : function(selector, value ) {
                $(selector).val(value);
            }

        }

        // this function is called by the iframe to register the iframe in the parent
        window.registerIFrame = function( iframeHelper ) {
            //if you have multible iframe in the parent you could change the code here to store them in e.g. an arrray or with a key in an object
            iframe = iframeHelper;

            // register the parent in the iframe
            iframe.registerParent(parentConnector);
        }



        $(document).on("click",".slideUp",function(event) {
            event.preventDefault();

            /*
            call a function on the helper object, this is the savest way for the commincation because
            there is it minimizes the risk to mix the differnt context
            */
            iframe.slideUp();

            /*
            This way would work at least in chrome, with the current version with jquery, but it relays the jQuery implementation
            that the correct document is used.
            */
            iframe.$(".info-from-parent").val("slideUp");

            /*
            One thing you should NEVER do is the following:

            iframe.$(".info-from-parent").append( $("<div></div>") );

            The reason you should not do this is because this mixes DOMElements of different context.
            This will result in unexpected crashes/bahaviors in some browsers (but could work in others, or at least it will look like it would work)
            */

        });


        // same as the slide down, it is just there to have some interaction sample
        $(document).on("click",".slideDown",function(event) {
            event.preventDefault();
            iframe.slideDown();
            iframe.$(".info-from-parent").val("slideDown");
        });


    })(jQuery, window);

    </script>
</head>
<body>
    <input type="text" class="info-from-iframe"><br>
    <a href="#" class="slideUp">slideUp</a> <a href="#" class="slideDown">slideDown</a><br>
    <iframe src="iframe.html"></iframe>

</body>
</html>

iframe的代码

<!doctype html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>iframe</title>
    <script src="jquery.js" type="text/javascript" charset="utf-8"></script>
    <script>

    (function($,window,undefined) {

        //the connector object the will be passed to the parent and will be used as "API"
        var iframeConnector = {

            _window : window,     // a reference to the iframes window (not really necessary but nice to have)
            _parent : undefined,
            $ : $,                // a reference to the iframes jQuery that can be accessed from the parent (use with caution)

            // this function is called by the parent to register the parent in the iframe
            registerParent : function( parent ) {
                this._parent = parent;
            },

            /* here you will define you "API" for the iframe (the functions you want to call from the parent )*/
            slideUp : function() {
                $(".test").slideUp();
            },

            slideDown : function() {

                $(".test").slideDown();
            },

            setElementValue : function(selector, value ) {
                $(selector).val(value);
            }

        };

        // register the iframe in the parent
        window.parent.registerIFrame(iframeConnector); 

        $(document).mousemove(function(event) {
            //use the parents "API" to call a function in the parent
            iframeConnector._parent.setElementValue(".info-from-iframe", event.clientX+", "+event.clientY);
        });

    })(jQuery,window);


    </script>
</head>
<body>
    <input type="text" class="info-from-parent"><br>
    <div class="test">
        iframe
    </div>
</body>
</html>


文章来源: Javascript: Can data be passed bi-directionally through an iframe?