function PanelChange(p) {
            if (p.style.display == "")
                p.style.display = "none";
            else
                p.style.display = "";
        }页面有好几个Panel, 然后:
<input id="Button1" type="button" value="隐藏" onclick="PanelChange("<%=Panel1.ClientID%>")" />
一打开这个页面就提示出错:行34 语法错误。
点击按钮不能隐藏Panel 到底错在哪????

解决方案 »

  1.   

    整个页面的代码是:<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 PanelChange(p) {
                if (p.style.display == "")
                    p.style.display = "none";
                else
                    p.style.display = "";
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <input id="Button1" type="button" value="隐藏" onclick="PanelChange("<%=Panel1.ClientID%>")" />
        <asp:Panel ID="Panel1" runat="server" Height="176px">
            <asp:Image ID="Image1" runat="server" ImageUrl="~/photo/1.jpg" />
        </asp:Panel>
        <asp:Panel ID="Panel2" runat="server">
            <asp:Image ID="Image2" runat="server" ImageUrl="~/photo/2.jpg" />
        </asp:Panel>
            <asp:Panel ID="Panel3" runat="server">
            <asp:Image ID="Image3" runat="server" ImageUrl="~/photo/3.jpg" />
        </asp:Panel>
        </form>
    </body>
    </html>
      

  2.   

    <input id="Button1" type="button" value="隐藏" onclick="PanelChange(" <%=Panel1.ClientID%>")" /> 
    换为
    <input id="Button1" type="button" value="隐藏" onclick="PanelChange(' <%=Panel1.ClientID%>')" /> 
    双引号匹配的问题
      

  3.   

    把双引号改成单引号会出错的:
    行10
    错误:'style.display'为空或不是对象如果去掉又引号,却是正确的
    怎么回事吗?
      

  4.   

    <input id="Button1" type="button" value="隐藏" onclick="PanelChange('<%=Panel1.ClientID%>')" />或者:<input id="Button1" type="button" value="隐藏" onclick='PanelChange("<%=Panel1.ClientID%>")' />
      

  5.   

     
    因为你的P只是ID,不是一个OBJECT
    <script type="text/javascript">
            function PanelChange(p) {
    obj=document.getElementById(p)
                if (obj.style.display == "")
                    obj.style.display = "none";
                else
                    obj.style.display = "";
            }
        </script>
      

  6.   

    因为你的P只是ID,不是一个OBJECT ,加多一个document.getElementById
    <script type="text/javascript"> 
            function PanelChange(p) { 
    obj=document.getElementById(p);
                if (obj.style.display == "") 
                    obj.style.display = "none"; 
                else 
                    obj.style.display = ""; 
            } 
        </script> 
      

  7.   

     function PanelChange(cid) {
               var p=document.getElementById(cid);
                if(p==null)
                  return;
                if (p.style.display == "")
                    p.style.display = "none";
                else
                    p.style.display = "";
            }你传的控件ID,当然js代码报错了,需要获取控件对像之后才能操作
      

  8.   

    最正规的写法:<input id="Button1" type="button" value="隐藏" onclick="PanelChange(document.getElementGyId('<%=Panel1.ClientID%>'))" />因为你function 中的参数传的是一个对象,所以要使用document.getElementById('...')来获取这个对象.
    关于你说的去掉引号就可以执行,是因为这是个别浏览器的默认行为(把控件的id默认为这个控件的对象).
      

  9.   

    onclick="PanelChange(<%=Panel1.ClientID%>)"
    这样才行,我也不知道为什么- -!
      

  10.   


    那你也太“新”了,加上个var才和javascript语法(使用变量之前要var声明一下)。实际上,看回复只要理解意思就好了,不能够仅仅简单模仿。
      

  11.   


    ie支持直接写的dom对象的id来引用,不需要写document.getElementById('....')。当然,实际上我还是建议你写繁复的那种。javascript就是这个“德性”,你通常必须写最繁复臃肿的那种代码才“兼容”性更好一些。
      

  12.   

       function PanelChange(p) {
                if (p.style.display == "")
                    p.style.display = "none";
                else
                    p.style.display = "";
            }
    你在的p只是一个ID Name
    你要么用document.getElementById(p)
    要么直接在
    <input id="Button1" type="button" value="隐藏" onclick="PanelChange(this)" /> 
      

  13.   

    把某个控件作为javascript函数的参数,直接给它控件ID就可以了
    我在CS中写的
    ddlSearch.Attributes.Add("onchange", "ddlSearchchange(this,textEmpNo,textEmpName);");
    this就是自身控件ddlSearch
    这样传回的就是一个对象了,不用再去obj=document.getElementById(p) 取得了
      

  14.   

    js的兼容性问题必然是浏览器大革命的产物...除非微软或者Mozilla其一垄断全球浏览器...
      

  15.   

    这一句改成这样子:
    <input id="Button1" type="button" value="隐藏" onclick="PanelChange('<%=Panel1.ClientID%>')" /> 
    看出跟你原句的区别了吗,就是把你的双引号换成单引号
      

  16.   

      function PanelChange(p) {
                if (p.style.display == "") //这地方有问题
                    p.style.display = "none";
                else
                    p.style.display = "";
            }
    改正为
    function PanelChange(p)
    {
         if(document.getElementById(p).style.display == "none")
        {
            document.getElementById(p).style.display = "";
        }
       else
       {
          doucument.getElementById(p).style.display ="none"
       }
    }
    <input id="Button1" type="button" value="隐藏" onclick='PanelChange( "<%# Panel1.ClientID%>")' />