JavaScript 错误“对象不支持此属性或方法”在 document.getElementByID 上?
当用户单击面板(在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
JavaScript 区分大小写。
document.getElementByID()
不是函数,但document.getElementById()
是。javascript is case sensitive.
document.getElementByID()
is not a function, butdocument.getElementById()
is.大小写很重要。
Capitalization matters.
它应该是 document.getElementById 而不是 document.getElementByID。
It should be document.getElementById not document.getElementByID.
JavaScript 区分大小写。尝试 document.getElementById 而不是 document.getElementByID
javascript is case sensitive. try document.getElementById instead of document.getElementByID
您应该将 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.