简体   繁体   English

对象不支持此属性或方法-IE 7/8

[英]Object doesn't support this property or method - IE 7/8

Any idea why this won't work in IE 7/8? 知道为什么这在IE 7/8中不起作用吗? (works fine in IE 9 and 10, FF and Chrome) (在IE 9和10,FF和Chrome中可以正常工作)

When I click the "Send" button, the console shows : 当我单击“发送”按钮时,控制台显示:

SCRIPT438: Object doesn't support this property or method 
script.1383689376.js, line 94 character 3
(Line 94) : token = $("#token").val();

HTML: HTML:

<div class="comment_heading">Leave a Comment</div>
<div class="post_comment">
<textarea name="txtpostcomment" id="txtpostcomment-'.$postid.'" class="txtpostcomment"></textarea>
<button class="btnpostcomment" id="btnpostcomment-'.$postid.'" onclick="comment('.$postid.');" type="button">Send</button>
<input type="hidden" name="token" id="token" value="'.$_SESSION['token'].'">
<script>document.getElementById("txtpostcomment-'.$postid.'").focus();</script>
</div>

Script: 脚本:

comment = function(postid1)
{
    txt =  $('#txtpostcomment-'+postid1);
    btn =  $('#btnpostcomment-'+postid1);

    comment1 = $(txt).val();
    token = $("#token").val();

    $(btn).css('background-image', 'url(/comments/submit-busy.gif)');
    $(btn).attr('disabled', true);
    $(btn).attr('disabled', true);
    ....
    ....
}

This line of HTML: 这行HTML:

<input type="hidden" name="token" id="token" value="'.$_SESSION['token'].'">

creates a property of the global object named "token" that is a reference to the input element. 创建名为“ token”的全局对象的属性,该属性是对输入元素的引用。

In this line: 在这一行:

  token = $("#token").val();

you have an undeclared identifier. 您有一个未声明的标识符。 When this line is executed, IE tries to create a global variable token (because of the undeclared variable) but because there already is one (the aforementioned DOM element), IE throws an error. 当执行此行时,IE尝试创建全局变量标记 (由于未声明的变量),但是由于已经存在一个(上述DOM元素),因此IE会引发错误。

Why IE doesn't simply assign the new value is a question that's been asked for over a decade, you won't get a sensible answer. 为什么IE不能简单地分配新值是一个已经问了十多年的问题,您将无法得到一个明智的答案。

The simple fix is to declare all variables . 简单的解决方法是声明所有变量

It is particularly bad to use a function expression to assign to an undeclared variable. 使用函数表达式分配给未声明的变量特别糟糕。 It has zero benefits over a function declaration and has some serious drawbacks (you've just discovered one). 与函数声明相比,它的好处为零,并且有一些严重的缺点(您刚刚发现了一个缺点)。 So always declare variables in an appropriate scope and always use function declarations unless you have a very good reason to use a function expression. 因此,除非有充分的理由使用函数表达式,否则请始终在适当的范围内声明变量,并始终使用函数声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM