2017-06-09 7 views
-1

参照リンクを見ると、jQueryの親参照にアクセスするiframeがあります。 iframeの代わりにそれが子ウィンドウだった場合、これと同じことが可能ですか?子ウィンドウ内で親からjqueryを使用する方法

P.S.
構文エラーの原因がわかりませんが、エラーを繰り返してもクリックハンドラーが機能しますか?

構文エラー
それはそう、私は私の警告に届かない$('#Button1').で呼ばれていたとき、私はreturn window.opener.$上の非記述構文エラーを取得しています。

親ウィンドウ

<html> 
<body> 

    <input id="Parent_Button1" type="button" value="Pop out window" /> 

    <script> 
     window.onload = function() { 
      $('#Parent_Button1').on('click', function() { 
       window.open('pop-out-win.aspx', '', 'width=400,height=300'); 
      }); 
     } 
    </script> 
</body> 
</html> 

子ウィンドウ

<input id="Button1" type="button" value="push me" /> 

<script> 
    window.onload = function() { 
     if (typeof (jQuery) == "undefined") { 
      var iframeBody = document.getElementsByTagName("body")[0]; 
      var jQuery = function (selector) { return window.opener.$(selector, iframeBody); }; 
      var $ = jQuery; 

      var btn1 = $('#Button1'); 

      btn1.on('click', function() { 
       alert('achievement unlocked!') 
      }); 
     } 
    } 
</script> 

参照
https://stackoverflow.com/a/10950158/139698

+0

はい、手順は可能です。何を達成しようとしているのですか? – guest271314

+0

oops、 '$( '#Button1')'によって呼び出されたときに、 'return window.opener。$'に対して文法的でない構文エラーが出てきていることに言及しませんでした。だから、私は警戒に迷っている。 – Rod

+0

[他のiFrameからiFrameの内容を消去する方法](https://stackoverflow.com/questions/33645685/how-to-clear-the-contents-of-an-iframe-from-another-iframe/)を参照してください。 )、[ユーザースクリプトを使用して共有Webワーカーをロードするにはどうすればよいですか?](https://stackoverflow.com/questions/38810002/how-can-i-load-a-shared-web-worker-with-a -user-script /) – guest271314

答えて

0

私は、ブラウザの互換性、readabiltyなどが、何については、このような解決しませんでした:

<body> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script> 
    function myFunction() { 
     var childWindow = window.open("", "childWindow", "width=200,height=100"); 
     var script = childWindow.document.createElement("script"); 
     script.innerHTML = 'if (typeof(jQuery) == "undefined") { window.jQuery = function(selector) { return window.opener.jQuery(selector, document);};jQuery = window.opener.$.extend(jQuery,window.opener.$);window.$ = jQuery;var msg = "Created with jQuery!";$("body").append(\'<button onclick="alert(msg)">Text</button>\');}'; 
     childWindow.document.getElementsByTagName("head")[0].appendChild(script); 
    } 

    </script> 
    <p>Click to test child window jQuery injection.</p> 

    <button onclick="myFunction()">Try it</button> 
</body> 

これが読める形式で上記のスクリプトタグの内容です:

if (typeof(jQuery) == "undefined") { 
    window.jQuery = function(selector) { 
     return window.opener.jQuery(selector, document); 
    }; 
    jQuery = window.opener.$.extend(jQuery, window.opener.$); 
    window.$ = jQuery; 
    var msg = "Created with jQuery!"; 
    $("body").append('<button onclick="alert(msg)">Text</button>'); 
} 

作業をFiddle

関連する問題