简体   繁体   English

document.getelementbyid无法正常工作

[英]document.getelementbyid not working

I wanted to hide some stuff by accessing to the iframe using javascript so I wrote this javascript function. 我想通过使用javascript访问iframe来隐藏一些东西,所以我写了这个javascript函数。

function changeCSS(){ 
frame = document.getElementById('frame1'); 
frame.contentWindow.document.getElementById('GlobalTitleAreaImage').style.display='none'; 
}

I then called the function when I load the iframe (onload= javascript: changecss()) 然后,当我加载iframe时调用了该函数(onload = javascript:changecss())

Everything seems to work fine in IE but in Safari it does't work. 一切似乎在IE中都可以正常工作,但在Safari中却无法正常工作。 Any ideas why it is behaving this way and how I should fix this. 任何想法为什么它会以这种方式以及我应该如何解决。

Thanks 谢谢

Note that both the frame and the location of the Javascript itself must be on the same domain for this to work. 请注意,Javascript本身的框架和位置都必须位于同一域中,这样才能起作用。 Safari and IE may handle it differently if both are from the file protocol, also. 如果Safari和IE均来自文件协议,则它们的处理方式也可能不同。

For ones that are not in the same domain look into cross-domain communication with iframes and the potential HTML 5 solution, wrapped nicely with fallback methods by libraries & plugins such as porthole , xssinterface , easyxdm , and others. 对于不在同一域中的应用程序,请查看与iframe和潜在HTML 5解决方案的跨域通信,并通过库和插件(如portholexssinterfaceeasyxdm等)的后备方法很好地包装了它们

While Safari is listed as supporting content Window, Chrome is not. 虽然Safari被列为支持内容的Window,但Chrome没有。 For cross-browser compatibility use a combination of contentWindow and contentDocument to ensure you hit on a supported property. 为了实现跨浏览器的兼容性,请结合使用contentWindow和contentDocument来确保您点击了受支持的属性。 So try: 因此,请尝试:

function changeCSS(){ 
    var frame = document.getElementById('frame1'); 
    var content = frame.contentWindow || frame.contentDocument;
    content.document.getElementById('GlobalTitleAreaImage').style.display='none'; 
}

如果iframe不在同一个域中,则出于安全原因,您不允许这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM