我有一个iframe,其中我有以下代码:
$(document).ready(function(){
$('.checksum').click(function(){
window.parent.Clickarunsuministro($(this).val());
})
})
当您点击.checksum
时,我想调用父项中的函数。它在Chrome和IE中完美运行,但在Firefox中无法运行。
出现的错误是:
Permission denied to access property 'Clickarunsuministro'
任何想法可能是什么问题?
答案 0 :(得分:0)
出于安全原因,通常不允许将iframe及其运行的页面直接进行交互。相反,您可以使用message
事件在拥有文档和iframe之间发送字符串和json对象。有关如何以及原因的详细信息,请参阅https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage?redirectlocale=en-US&redirectslug=DOM%2Fwindow.postMessage。
您的代码现在可以在Chrome和IE中使用,但可以肯定的是,它很快就会出现这种情况,因此在这种情况下,您可以更好地保护您的代码。
答案 1 :(得分:0)
您可以尝试使用jquery自定义事件
在iframe中
$(document).ready(function(){
$('.checksum').click(function(){
$(window).trigger('clickarunsuministro', {data: $(this).val()})
})
})
在父母
中$('<frame-selector>').on('clickarunsuministro', function(event){
console.log('data', event.data)
})