Como vincular dois objetos documentos à função de jQuery único
-
23-09-2019 - |
Pergunta
Eu tenho uma função jQuery personalizada que preciso vincular a dois objetos do documento iframe.
Atualmente, ele funcionará com apenas um fazendo algo como:
$(window.frames["iframeName"].document).bind('textselect', function(e) {
});
O que estou procurando fazer é algo como:
$(window.frames["iframeName"].document,window.frames["iframeName2"].document).bind('textselect', function(e) {
});
Nenhuma solução correta
Outras dicas
Você pode converter a função anônima para um nomeado e usá -lo para ambos os binds, como este:
$(window.frames["iframeName"].document).bind('textselect', selectStuff);
$(window.frames["iframeName2"].document).bind('textselect', selectStuff);
function selectStuff(e) {
//Stuff
}
Em vez de usar uma função anônima - crie uma função nomeada
function myhandler(e)
{
//body of function
}
Em seguida, use a função nomeada:
$(window.frames["iframeName"].document).bind('textselect', myhandler);
$(window.frames["iframeName2"].document).bind('textselect', myhandler);
Outra 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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow