我本想做个想win7旗舰版桌面能自动换背景的网页,先是做了一个可是却出问题,求指导
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="Model.WebForm5" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">    
    function change() {
        document.getElementById("bg").style.backgroundImage = "url(images/show_fengjingta_236621_4.jpg)";
    }
    setTimeout(change(), 1000)
    </script>
</head><body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div id = "bg">
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
            </asp:Timer>
        </ContentTemplate>        
        </asp:UpdatePanel>
        
    </div>    
    </form>
</body>
<div>
</div>
</html>运行时却说无法找到style值 这是为什么

解决方案 »

  1.   


        function change() {
            document.getElementById("bg").style.backgroundImage = "url(images/show_fengjingta_236621_4.jpg)";
        }
        window.onload = function(){
            setTimeout(change(), 1000)
        }
      

  2.   


    setTimeout(change, 1000); //去掉()后在FF和IE6都可以执行。
      

  3.   

    2楼正解setTimeout第一个参数是一个方法,你加上括号了已经执行了这个方法,当执行了这个方法的时候这个div还没加载,所以报错。就算像我说的那样加上window.onload,函数先执行,换掉背景后,再执行setTimeout,函数返回的又是undefined,应该又要搞错。
      

  4.   

    不过建议还是加上window.onload改成
    window.onload = function(){
        setTimeout(change, 1000);
    }不然要是1秒后div还是没有加载到,还是会报错
      

  5.   

    正解,html是一行一行解析执行的,所以需要对html元素进行操作时,最后等到html元素都加载完,也就是放在onload事件中,或者说jquery的ready事件中,如果不想这么做,那也要把js代码放到html页面的最下面,”运行时却说无法找到style值 “可定是dom元素还没有加载出来