【问题标题】:'document.Form1.imgLoading' is null or not an object'document.Form1.imgLoading' 为 null 或不是对象
【发布时间】:2014-03-26 12:07:52
【问题描述】:

有人可以帮忙吗,我遇到以下错误:

'document.Form1.imgLoading' 为 null 或不是对象 control.js,第 55 行字符 2

这是:

第 55 行:document.Form1.imgLoading.style.width=370

发件人:

function setLoading(){
    var strPath = "../images/"
    var strFName = "searchingText.gif"
    var strSearchPath = ""

    //15/2if (!document.Form1.TextBoxLangID.value == "") { //SO LANG USED
    //  strSearchPath = strPath + document.Form1.TextBoxLangID.value + "/brand/" + strFName
    //}
    //else{
    //  strSearchPath = strPath +  "brand/" + strFName
    //15/2}

    //document.Form1.imgSearchingText.src = strSearchPath
    document.Form1.imgLoading.style.width=370
    document.Form1.imgLoading.style.height=328
    document.Form1.imgLoading.style.left = 327
    document.Form1.imgLoading.style.top = 220
    document.Form1.imgLoading.style.zIndex=2000'

【问题讨论】:

  • 继承 index.aspx 中的 javascript
  • 表示您没有名为“imgLoading”的元素。
  • 为什么这只是在虚拟机上运行时才会出现问题,它也在其他虚拟机上运行而没有错误。这是 IIS 问题吗?
  • 无法用您提供的内容告诉您原因。

标签: javascript asp.net asp-classic controls


【解决方案1】:

你的代码的问题是你引用form.controlname。在 ASP.Net 中,控件名称是根据母版页/嵌套控件配置生成的,如 ctl$imgLoading 等。

最好避免document.Form1.imgLoading 并利用它来引用它 基于id使用原生document.getElementById()

如果您在.aspx页面中有脚本代码,请使用下面的代码引用元素

var imgLoading=document.getElementById('<%= imgLoading.ClientID %>');
imgLoading.style.width=370;

如果你有script in JS file 确保为 .aspx 页面中的 imgLoading 控件设置了 ClientIDMode=staticSupported from ASP.Net 4.0 only,然后

var imgLoading=document.getElementById('imgLoading');
imgLoading.style.width=370;

【讨论】:

  • 谢谢Murail,我会记住的!
【解决方案2】:

我没有足够的声誉发表评论,但我只想说,如果您的&lt;script&gt; 标签在&lt;head&gt; 部分,请将您的脚本标签放在正文部分的后面,或者加载脚本@ 987654323@ 的文件。

这是因为您文档的&lt;body&gt; 部分中的图像尚未加载,所以它是null or not an object

希望对您有所帮助。

【讨论】:

    【解决方案3】:

    如果 imgLoading 是一个 asp:Image 控件,那么您必须使用下一个代码找到它:

    document.getElementById('<%= imgLoading.ClientID %>').style.width = 370
    

    【讨论】:

    • 我刚试过这个,它给了我'对象不支持这个属性或方法'
    猜你喜欢
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多