0

メインフレームと iframe があります。

  1. 関数 beforeIframe を mainFrame に渡します。
  2. mainFrame beforeIframe の URL を changedIFrame に変更します。
  3. changedIFrame がロードされたら、無名関数を実行します。

以下のコードは私がやりたいことです:

mainFrame.jsp

function randerLeftMenu(callBack) {
    $("#ifrm").attr("src", "<ui:context />/changedIframe")
    if(callBack) {
        $("#ifrm").load(function() { callBack(); });
    }
}

beforeIframe.jsp

(DOM オブジェクトを操作する必要があります。)

parent.randerLeftMenu(function() {$("#docType").val("aaa"); });

changedIframe.jsp

<input type="text" value="testVal" id="docType" class="input width2">

私は多くのことを試しましたが、それを達成することはできません。

メインフレーム

function randerLeftMenu(callBack) {
    if(callBack) {
        $("#ifrm").load(function() {
            with(this.contentWindow) { callBack(); }
            callBack.apply(this);
            eval(callBack());
            callBack.call(this);
            $.proxy(callBack(),this.contentWindow);
        });
    }
}
4

1 に答える 1

0

あなたの例では、 「beforeIframe.jsp」$の呼び出しに含めているのは、randerLeftMenu常にそれが呼び出されたコンテキストにスコープされます。this変数のみを動的に別のスコープにバインドできます。Functionただし、関数を文字列として渡し(またはのtoString()メソッドに依存して)、文字列を動的に評価することで、これを回避できるはずです。

動作例を含むように更新されました(テスト済みのFirefoxおよびIE):

function randerLeftMenu(callBack) {
    $("#ifrm").attr("src", "<ui:context />/changedIframe");
    if(callBack) {
        $("#ifrm").load(function(e) {
            try {
                // This script injection and setTimeout is only necessary
                //  if you don't want to change the iframe source to add jQuery;
                //  otherwise, you only need the eval().
                var script = document.createElement('script');
                script.src = "http://code.jquery.com/jquery-latest.js";
                e.target.contentWindow.document.body.appendChild(script);
                setTimeout(function () {
                    // var evalStr = 'e.target.contentWindow.eval(\'('+callBack.toString().replace(/\n/g, ' ')+')()\')'; // You can pass the function and convert to a string for Firefox, but IE of course doesn't support it, so you will need to pass the function as a string
                    var evalStr = 'e.target.contentWindow.eval(\'('+callBack.replace(/[\r\n]/g, ' ')+')()\')';
                    eval(evalStr);
                }, 500);
            }
            catch(e) {
                alert(e);
            }
        });
    }
}
于 2012-09-20T01:59:46.267 に答える