javascript JS 错误:预期的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6033546/
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 19:13:54  来源:igfitidea点击:

JS error: object expected

javascript

提问by oJM86o

I load an internal page that I developed in IE and at the bottom it displays some JS error: It says Line 107 character 6. I look at the JScript file and it has this code:

我加载了一个我在 IE 中开发的内部页面,它在底部显示了一些 JS 错误:它说第 107 行字符 6。我查看了 JScript 文件,它有以下代码:

 function isLessThanStartDate(obj)
    {
     var startdate = new Date(document.getElementById('txtSD').value);
     var enddate = new Date(obj.value);
     var weekending = new Date(document.getElementById('txtWE').value);

     if (!(isDate(startdate)))
   {
        obj.style.backgroundColor="red";
        alert (obj.value + " is not a valid date!");
        obj.value="";
        return;
   }

     if (enddate < startdate)
      {
      obj.style.backgroundColor="red";
      alert ("End date: " + enddate + " cannot be less then start date: " + startdate);
      obj.value="";
      }
     else if (enddate > weekending)
     {
      obj.style.backgroundColor="red";
      alert ("End date: " + enddate + " cannot be greater then week ending date: " + weekending);
      obj.value="";
     }
      else
      {
      obj.style.backgroundColor="";
      }      
    }

Line 107 is the line where it says

第 107 行是它说的那一行

var weekending = new Date(document.getElementById('txtWE').value);

Why is this complaining? I don't see anything wrong with it...

为什么这是抱怨?我看不出有什么问题...

回答by David Ruttka

Oftentimes, an error will tell you exactly what is wrong and where. This is the case here.

通常,错误会准确地告诉您什么地方错了。这是这里的情况。

We can tell what isn't wrong on the line.

我们可以判断线路上没有什么问题。

  1. First, we know we have documentbecause we'd have larger problems if we didn't, and we'd find out two lines earlier. Scratch that.
  2. Next, we know that the value passed to the Date constructor doesn't matter. new Date("foo")will throw no error, it will just create a Datewhich when alerted reports "Invalid Date". Even new Date(null)is ok.
  3. Assignment can't throw this exception.
  1. 首先,我们知道我们有,document因为如果我们没有,我们会遇到更大的问题,而且我们会更早发现两行。抓那个。
  2. 接下来,我们知道传递给 Date 构造函数的值无关紧要。new Date("foo")不会抛出任何错误,它只会Date在收到警报时创建一个报告"Invalid Date"。甚至new Date(null)还可以。
  3. 赋值不能抛出这个异常。

This leaves us with very little to check out. As others have suggested, document.getElementById('txtWE')must not be returning the object you're expecting. Knowing this, see this jsfiddle. If I "break" the input with id="txtWE" to be any other ID, I get your error.

这让我们几乎没有什么可以检查的。正如其他人所建议的,document.getElementById('txtWE')一定不要返回您期望的对象。知道这一点,请参阅此jsfiddle。如果我将 id="txtWE" 的输入“破坏”为任何其他 ID,我会收到您的错误信息。

Are you sure that txtWE is there?

你确定 txtWE 在那里?

回答by WEFX

If your error is in IE, you can right-click on the page and View Source. Then, check THAT page for line 107. It will probably be different than that "var weending..." code. Or, if it is in fact the 107 code you posted, perhaps a Date can't be created from the txtWE value. Are you validating that txtWE input in any way?

如果您的错误是在 IE 中,您可以右键单击该页面并查看源代码。然后,检查该页面的第 107 行。它可能与“var weending...”代码不同。或者,如果它实际上是您发布的 107 代码,则可能无法从 txtWE 值创建日期。您是否以任何方式验证 txtWE 输入?

回答by Dan Davies Brackett

From the coment on the question, the error is 'object required'. That usually means that you're trying to access a property of something that's null.

从对该问题的评论来看,错误是“需要对象”。这通常意味着您正在尝试访问空值的属性。

Your line 107 might do that, though, if document.getElementById('txtWE')returned null. When you run just that code, at the console, do you get an element or do you get null? To put it another way, please check whether you have an element in the page whose ID (not name!) is txtWE.

但是,如果document.getElementById('txtWE')返回 null ,您的第 107 行可能会这样做。当您只运行该代码时,在控制台中,您得到一个元素还是得到 null?换句话说,请检查页面中是否有 ID(不是名称!)为 的元素txtWE