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

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

本文关键字:IE 方法 不支持 属性 对象      更新时间:2023-09-26

知道为什么这在IE 7/8中不起作用吗?(在IE 9和10,FF和Chrome中工作正常)

当我单击"发送"按钮时,控制台显示:

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

.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>

脚本:

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);
    ....
    ....
}

这行 HTML:

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

创建名为"token"的全局对象的属性,该属性是对输入元素的引用。

在这一行中:

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

您有一个未声明的标识符。执行此行时,IE 尝试创建一个全局变量令牌(因为未声明的变量),但由于已经有一个(前面提到的 DOM 元素),IE 会抛出错误。

为什么IE不简单地分配新值是一个十多年来一直被问到的问题,你不会得到一个明智的答案。

简单的解决方法是声明所有变量

使用函数表达式赋值给未声明的变量尤其糟糕。与函数声明相比,它没有任何好处,并且有一些严重的缺点(您刚刚发现了一个)。因此,始终在适当的范围内声明变量,并始终使用函数声明,除非您有很好的理由使用函数表达式。