Cómo enlazar dos objetos de documento a la función jQuery sola
-
23-09-2019 - |
Pregunta
I tiene una función jQuery costumbre que tengo que unen a dos objetos de documento marco flotante.
En la actualidad va a trabajar con un solo haciendo algo como:
$(window.frames["iframeName"].document).bind('textselect', function(e) {
});
lo que estoy tratando de hacer es algo como:
$(window.frames["iframeName"].document,window.frames["iframeName2"].document).bind('textselect', function(e) {
});
No hay solución correcta
Otros consejos
Puede convertir la función anónima a una llamada y utilizarlo para ambas une, de esta manera:
$(window.frames["iframeName"].document).bind('textselect', selectStuff);
$(window.frames["iframeName2"].document).bind('textselect', selectStuff);
function selectStuff(e) {
//Stuff
}
En lugar de utilizar una función anónima - crear una función llamada
function myhandler(e)
{
//body of function
}
A continuación, utilice la función de llamada:
$(window.frames["iframeName"].document).bind('textselect', myhandler);
$(window.frames["iframeName2"].document).bind('textselect', myhandler);
Otra 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>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow