日期:2014-05-17  浏览次数:20617 次

写了一段js代码,在chrome浏览器下可以实现,但是在IE下不能实现. 请教高人~~
写了一段js代码,在chrome浏览器下可以实现,但是在IE下不能实现. 请教高人~~
以下为代码:
<script type="text/javascript">
    window.onload=function(){
var floatWin = document.createElement("div");
        floatWin.setAttribute("style","z-index:2;position:absolute;height:80px;width:150px;left:2px;top:2px;cursor:pointer;background-image:url(image/floatBg3.jpg);-webkit-border-radius: 10px 10px 10px 10px;-moz-border-radius: 10px 10px 10px 10px;-o-border-radius: 10px 10px 10px 10px;-ms-border-radius: 10px 10px 10px 10px;border-radius: 10px 10px 10px 10px;");
var oneButton = document.createElement("input");
oneButton.setAttribute("type","button");
oneButton.setAttribute("value","x");
if (oneButton.style.cssFloat)
{
oneButton.style.cssFloat="right"
}
else
{
oneButton.style.cssFloat="right"
}
floatWin.appendChild(oneButton);


var txtDiv = document.createElement("div");
txtDiv.innerHTML="<a id='floata'>happy birthdyay</a>";
txtDiv.setAttribute("style","position:absolute;margin-top:25px;font-size:14px;color:#f30;");

floatWin.appendChild(txtDiv);
document.body.insertBefore(floatWin,wrapper);
var x = 1;
var y =1;

//alert(document.body.scrollHeight);
//定时器
var timer = setInterval(move,10);
//move方法
function move(){
var tops = floatWin.offsetTop;
            var lefts = floatWin.offsetLeft;
if(lefts >= screen.width - parseInt(floatWin.style.width)-20||lefts<=0){
x = -x;
}
if(tops >= document.body.scrollHeight - parseInt(floatWin.style.height) - 5 ||tops<=0){
y = -y;
}
lefts = lefts + x;
tops = tops + y;
floatWin.style.left = lefts + "px";
floatWin.style.top = tops + "px";
}
//结束move方法
//悬停停止
floatWin.onmouseover=function(){
clearInterval(timer);
}
//放手继续运动
floatWin.onmouseout=function(){
timer =setInterval(move,10);
}
//删除
oneButton.onclick=function(){
document.body.removeChild(floatWin);
}
};


</script>

------解决方案--------------------
 floatWin.setAttribute("style",value)

这种代码改为

floatWin.style.cssText =value

IE不支持style用setAttribute设置