dom 引用仅在 IE 中正常工作
我有一个 javascript 函数,我从页面中的图像 onClick 事件调用该函数。
基本上,该函数设置一个引用同一页面内元素的变量。
下面是该函数的调用方式(注意,html 是使用 PHP 打印的,但不要认为这对 html 本身有任何影响):
echo "<img src='site/images/apps/show.gif' onClick='apps()' id='appbutton' style='#'>";
这是脚本引用的内容:
<iframe frameborder="0" name="app_frame" src="site/apps.html" width="0%" height="100%" scrolling="no"></iframe>
最后,这是它的引用方式以及内容完成了:
<script type="text/javascript">
function apps()
{
var element = document.getElementById("app_frame");
if (element.width == '0%')
{
parent.document.getElementById("frame").setAttribute("width","100%");
parent.document.getElementById("app_frame").setAttribute("width","0%");
parent.document.getElementById("appbutton").setAttribute("src","site/images/apps/show.gif");
parent.document.getElementById("wthrbutton").style.visibility="hidden";
}
else
{
parent.document.getElementById("frame").setAttribute("width","65%");
parent.document.getElementById("app_frame").setAttribute("width","35%");
parent.document.getElementById("appbutton").setAttribute("src","site/images/apps/hide.gif");
parent.document.getElementById("wthrbutton").style.visibility="visible";
}
}
</script>
主要问题在函数的第一行,var element = document.getelementbyid。
Firefox、Chrome 和 Safari 都存在这个问题,而且它们似乎都没有设置该变量,这使得脚本的其余部分毫无用处,因为整个事情都是围绕变量展开的。
有人知道将该元素设置为可在这些浏览器中使用的变量的任何其他方法吗?
谢谢
I have a javascript function that I am calling from an image onClick event in my page.
Basically the function sets a variable which is referencing an element within the same page.
here is how the function is called (note, the html is printed using PHP, but don't think that has any effect on the html itself):
echo "<img src='site/images/apps/show.gif' onClick='apps()' id='appbutton' style='#'>";
here is what the script references:
<iframe frameborder="0" name="app_frame" src="site/apps.html" width="0%" height="100%" scrolling="no"></iframe>
and finally, here is how it is referenced and what is done with it:
<script type="text/javascript">
function apps()
{
var element = document.getElementById("app_frame");
if (element.width == '0%')
{
parent.document.getElementById("frame").setAttribute("width","100%");
parent.document.getElementById("app_frame").setAttribute("width","0%");
parent.document.getElementById("appbutton").setAttribute("src","site/images/apps/show.gif");
parent.document.getElementById("wthrbutton").style.visibility="hidden";
}
else
{
parent.document.getElementById("frame").setAttribute("width","65%");
parent.document.getElementById("app_frame").setAttribute("width","35%");
parent.document.getElementById("appbutton").setAttribute("src","site/images/apps/hide.gif");
parent.document.getElementById("wthrbutton").style.visibility="visible";
}
}
</script>
The main issue is on the first line of the function, var element = document.getelementbyid.
Firefox, Chrome and Safari all hve issues with this, and none of them seem to set the variable, which renders the rest of the script useless, as the whole thing revolves around the variable.
Anyone know any other way of setting that element as a variable that would work in these browsers?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为没有任何 id 为 app_frame 的内容。您已将 iframe 的名称设置为 app_frame。将 iframe 的代码更改为:
一篇文章指出 IE 中的此怪癖
MSDN 关于 getElementById 的文档声明它返回名称或 id
That is because there is nothing with an id of app_frame. You have set the iframe's name to app_frame. Change your iframe's code to:
An article pointing out this quirk in IE
MSDN's doc on getElementById states it returns names or id