Domanda

Ho un custom funzione jquery che ho bisogno di legarsi a due iframe oggetti del documento.

Attualmente funziona con appena uno fa qualcosa di simile:

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

});

quello che sto cercando di fare è qualcosa di simile a:

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

});

Nessuna soluzione corretta

Altri suggerimenti

È possibile convertire la funzione anonima a un nominato uno e utilizzarlo per entrambe le lega, in questo modo:

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

function selectStuff(e) {
  //Stuff
}

Invece di utilizzare una funzione anonima - creare una funzione con nome

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

Quindi utilizzare la funzione denominata:

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

Un'altra alternativa

<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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top