単一jqueryの関数に2つのドキュメントオブジェクトをバインドする方法

StackOverflow https://stackoverflow.com/questions/2542542

  •  23-09-2019
  •  | 
  •  

質問

私は、私は2つのiframeドキュメントオブジェクトにバインドする必要があることをカスタムjqueryの機能を持っています。

現在、それはちょうど1のようなものをやっていきます

$(window.frames["iframeName"].document).bind('textselect', function(e) {

});
私は何を探していることのようなものがあります:

$(window.frames["iframeName"].document,window.frames["iframeName2"].document).bind('textselect', function(e) {

});

正しい解決策はありません

他のヒント

あなたが名前のいずれかに無名関数を変換することができますし、このように、両方のバインドのためにそれを使用します:

$(window.frames["iframeName"].document).bind('textselect', selectStuff);
$(window.frames["iframeName2"].document).bind('textselect', selectStuff);

function selectStuff(e) {
  //Stuff
}
代わりに、匿名関数を使用しての

- という名前の関数を作成します。

function myhandler(e)
{
    //body of function
}

次に名前の関数を使用します:

$(window.frames["iframeName"].document).bind('textselect', myhandler);
$(window.frames["iframeName2"].document).bind('textselect', myhandler);

別の方法

<body>
    <iframe name="iframe1" src="text.html" width="100px" height="100px" style="border:1px solid #000"></iframe>
    <iframe name="iframe2" src="text.html" width="100px" height="100px" style="border:1px solid #000"></iframe>

    <script type="text/javascript">

    $(document).ready(function() {

        function bindFrameDocuments(eventType, handler) {
            for (var i = 0; i < window.frames.length; i++) {
                $(window.frames[i].document).bind(eventType, handler);
            }
        }

        bindFrameDocuments( 
            'textselect', 
            function(e) {
                // this function runs in the scope of the frame
                window.parent.console.log(e);
            } 
        );

    });

    </script>
</body>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top