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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7208859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:18:01  来源:igfitidea点击:

Javascript error 'Object doesn't support this property or method' on document.getElementByID?

c#javascriptasp.netinternet-explorer-8master-pages

提问by badpanda

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:

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

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

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

Here is the relevant code for the Master page ASPX:

这是母版页 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:

和内容页面 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.

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

Thanks!

谢谢!

badPanda

坏Pandas

回答by scottm

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

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

回答by Dennis

Capitalization matters.

资本化问题。

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

回答by Senad Me?kin

It should be document.getElementById not document.getElementByID.

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

回答by Chandra Sekaran V

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

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

回答by Pat Grady

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.

您应该将 getElementID 更改为 getElementById。我也这样做了,好几天都想不通。请记住,JavaScript 区分大小写并使用小写 CamelCase。