Javascript IE 错误,对象不支持此属性或方法

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

IE Error, Object doesn't support this property or method

javascriptjqueryinternet-explorer

提问by dubbelj

 function charCount(){
  $.doTimeout('poll', 150, function(){
      messageVal = $('#messageLabel textarea').val();

      messageLength = messageVal.length; //IE BREAKS HERE

      $('#messageLength').html(messageLength + '/140')
      if(messageLength > 140){
          $('#messageLength').not('.inv').addClass('inv')
      }else{
          $('#messageLength.inv').removeClass('inv')
      }
      return false;
  })

}
$('#messageLabel textarea').change(charCount).keyup(charCount);

Gives the following error in Internet Explorer 7.0 (and maybe other versions too).

在 Internet Explorer 7.0(可能还有其他版本)中出现以下错误。

Object doesn't support this property or method.

对象不支持此属性或方法。

Any ideas on what is causing this error?

关于导致此错误的原因的任何想法?

回答by Shadow Wizard is Ear For You

When you don't use the varkeyword, IE browser search for messageLengthin the global context and it finds it... you have element with that ID.

当您不使用var关键字时,IE 浏览器会messageLength在全局上下文中搜索并找到它……您拥有具有该 ID 的元素。

Trying to assign number to HTML element fails.

尝试为 HTML 元素分配编号失败。

To solve this, just declare messageLengthas local variable:

要解决这个问题,只需声明messageLength为局部变量:

var messageLength = messageVal.length; //IE WON'T BREAK HERE

回答by alexl

Try yo replace :

尝试替换:

messageVal = $('#messageLabel textarea').val();

with

messageVal = $('#messageLabel textarea').text();

Hope it helps.

希望能帮助到你。

回答by Ammu

I think the .change() is having some problem in IE.Please remove it and see if it is working.

我认为 .change() 在 IE 中存在一些问题。请删除它,看看它是否有效。

Also try using .html() instead of .val().

也可以尝试使用 .html() 而不是 .val()。

回答by AEMLoviji

look here simple test. textarea does not support value property. you can get it via text property

看看这里简单的测试。textarea 不支持 value 属性。您可以通过 text 属性获取它

回答by Adam

I had a similar error, however, it was because I added the JQuery library to the master page and there was a duplicate library declaration elsewhere the same page.

我有一个类似的错误,但是,这是因为我将 JQuery 库添加到母版页,并且在同一页面的其他地方有重复的库声明。