0

我收到此错误:

未捕获的语法错误:意外的令牌(

当我不注释掉这个函数时:

function setTextField(str)
{
        if ( (str == "") ||  (str == null) ) 
          str = "Enter Task Here";
        document.getElementById.("get_subject").value = str;
}

我正在尝试从其他地方执行此操作(稍后在代码中):

setTimeout('setTextField();', 1000);

为什么我会收到此错误?

4

1 回答 1

7
document.getElementById.("get_subject").value = str;
//                     ^ What's that doing there?

{token}.需要后跟一个作为属性名称的标记,才能成为有效的 JS 语法(不包括一些数字文字语法)。

你要:

document.getElementById("get_subject").value = str;

此外,永远,永远,永远,将字符串传递给setTimeout. 它需要一个真正的功能!

setTimeout(setTextField, 1000);

或者,如果您想执行更复杂的代码:

setTimeout(function() {
  setTextField(someArgument, someOtherArgument);
  //othercode
}, 1000);
于 2012-12-15T02:10:24.173 回答