JavaScript 错误“对象不支持此属性或方法”在 document.getElementByID 上?

发布于 2024-12-02 00:30:38 字数 2396 浏览 1 评论 0原文

当用户单击面板(在 TableCell 中)时,我尝试运行此 JS 函数。该面板是内容页中的一个元素,与具有内容占位符的母版页一起使用。当我尝试单击面板时,会引发以下错误:

Microsoft JScript 运行时错误:对象不支持此属性或方法

以下是母版页 ASPX 的相关代码:

       <link rel="Stylesheet" href="../includes/styles.css" />
       <script type="text/javascript" language="javascript">
           function swapDirections(control) {
               var main = document.getElementByID('ct100_TableContent_' + control);
               var long = document.getElementById('ct100_TableContent_' + control + '_long');
               var short = document.getElementById('ct100_TableContent_' + control + '_short');

               var mainhtml = main.innerHTML;
               var longhtml = long.innerHTML;
               var shorthtml = short.innerHTML;

               if (mainhtml.length == shorthtml.length)
                   main.innerHTML = longhtml;
               else
                   main.innerHTML = shorthtml;
           }
        </script>

以下是内容页的相关代码:

            Panel rigDirections = new Panel();
            rigDirections.CssClass = "clip";
            rigDirections.ID = u.Id.ToString() + "_RD";
            string MainDivRD = rigDirections.ClientID;
            Literal rigDir = new Literal();
            string sshort = "";
            if (u.RigDirections.ToLower().Length > (textCutOFF + 4))
            {
                sshort = Server.HtmlEncode(u.RigDirections.ToLower().Substring(0, textCutOFF).Trim()) + " ...";
                rigDir.Text = "<a class=RD_RA title=\"Click to Expand\" href=\"javascript: swapDirections('" + MainDivRD + "')\">" + sshort + "</a>"; ;
            }
            else
            {
                sshort = Server.HtmlEncode(u.RigDirections.ToLower().Trim());
                rigDir.Text = sshort ;
            }
            string slong = Server.HtmlEncode(u.RigDirections.ToLower().Trim());

            rigDirections.Controls.Add(rigDir);
            cell.Controls.Add(rigDirections);    

内容页 ASPX:

      <asp:Content ID="Content1" ContentPlaceHolderID="TableContent" Runat="Server">

       <asp:Panel ID="pnlTable" Width="950" runat="server">

       </asp:Panel>
      </asp:Content>

函数的第一行抛出错误。

我已经用尽了我的互联网搜索技能,而且我以前从未使用过 JS,所以如果有人有任何想法,我将不胜感激。

谢谢!

坏熊猫

I am attempting to run this JS function when a user clicks on a Panel (in a TableCell). This Panel is an element in a Content Page, which is used with the Master Page which has a Content Place Holder. When I try to click on the panel, the following error is thrown:

Microsoft JScript runtime error: Object doesn't support this property or method

Here is the relevant code for the Master page ASPX:

       <link rel="Stylesheet" href="../includes/styles.css" />
       <script type="text/javascript" language="javascript">
           function swapDirections(control) {
               var main = document.getElementByID('ct100_TableContent_' + control);
               var long = document.getElementById('ct100_TableContent_' + control + '_long');
               var short = document.getElementById('ct100_TableContent_' + control + '_short');

               var mainhtml = main.innerHTML;
               var longhtml = long.innerHTML;
               var shorthtml = short.innerHTML;

               if (mainhtml.length == shorthtml.length)
                   main.innerHTML = longhtml;
               else
                   main.innerHTML = shorthtml;
           }
        </script>

Here is the relevant code for the Content page:

            Panel rigDirections = new Panel();
            rigDirections.CssClass = "clip";
            rigDirections.ID = u.Id.ToString() + "_RD";
            string MainDivRD = rigDirections.ClientID;
            Literal rigDir = new Literal();
            string sshort = "";
            if (u.RigDirections.ToLower().Length > (textCutOFF + 4))
            {
                sshort = Server.HtmlEncode(u.RigDirections.ToLower().Substring(0, textCutOFF).Trim()) + " ...";
                rigDir.Text = "<a class=RD_RA title=\"Click to Expand\" href=\"javascript: swapDirections('" + MainDivRD + "')\">" + sshort + "</a>"; ;
            }
            else
            {
                sshort = Server.HtmlEncode(u.RigDirections.ToLower().Trim());
                rigDir.Text = sshort ;
            }
            string slong = Server.HtmlEncode(u.RigDirections.ToLower().Trim());

            rigDirections.Controls.Add(rigDir);
            cell.Controls.Add(rigDirections);    

And the Content Page ASPX:

      <asp:Content ID="Content1" ContentPlaceHolderID="TableContent" Runat="Server">

       <asp:Panel ID="pnlTable" Width="950" runat="server">

       </asp:Panel>
      </asp:Content>

The error is being thrown on the first line of the function.

I have exhausted my internet searching skills, and I have never used JS before, so if anyone has any ideas I would greatly appreciate them.

Thanks!

badPanda

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

思念满溢 2024-12-09 00:30:38

JavaScript 区分大小写。 document.getElementByID() 不是函数,但 document.getElementById() 是。

javascript is case sensitive. document.getElementByID() is not a function, but document.getElementById() is.

只是我以为 2024-12-09 00:30:38

大小写很重要。

document.getElementByID() //wrong
document.getElementById() //right

Capitalization matters.

document.getElementByID() //wrong
document.getElementById() //right
吃素的狼 2024-12-09 00:30:38

它应该是 document.getElementById 而不是 document.getElementByID。

It should be document.getElementById not document.getElementByID.

苍白女子 2024-12-09 00:30:38

JavaScript 区分大小写。尝试 document.getElementById 而不是 document.getElementByID

javascript is case sensitive. try document.getElementById instead of document.getElementByID

千と千尋 2024-12-09 00:30:38

您应该将 getElementID 更改为 getElementById。我也这么做了,搞了好几天也没搞明白。请记住,JavaScript 区分大小写并使用小写 CamelCase。

You should change getElementID to getElementById. I did this too and couldn't figure it out for days. Just remember that JavaScript is Case Sensitive and uses lowercase CamelCase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文