js设置元素时好时坏的问题

来源:互联网 发布:临沂淘宝培训 编辑:程序博客网 时间:2024/05/21 10:17

要把项目兼容到ie,先从10做起,判断若是ie10则进行调整操作:

if( "ActiveXObject" in window && document.documentMode>=10 ){            document.getElementById("username").style.position="relative";            document.getElementById("username").style.left="3px";        }

但发现这段代码时好时坏,一会起作用一会不起作用,后来才发现原来是放的位置不对,我把他放在了jquery的文档就绪函数中:$(function(){ …… });

这里的代码在页面没有完全加载之前就会运行,所以可能会出现找不到元素的情况,当然对其的设置也不会生效,它要放在页面各部分完全加载完之时或后才会起作用:

<script language="javascript" type="text/javascript">document.onreadystatechange = function(){    if(document.readyState=="complete"){        if( "ActiveXObject" in window && document.documentMode>=10 ){            document.getElementById("username").style.position="relative";            document.getElementById("username").style.left="3px";        }    }else{       alert("文档未完全加载");    }}</script> 


0 0