简体   繁体   English

IE8中的Javascript错误“未实现”

[英]Javascript error “Not implemented” in IE8

Am getting this error in IE8 F12 tools 在IE8 F12工具中遇到此错误

I am trying to write unobstrusive validation code, wherein I will call validate() method on body onload and it will attach validation code on particular events of the elements depending on its class name. 我正在尝试编写简洁的验证代码,其中我将在body onload上调用validate()方法,并且它将根据元素的类名称将验证代码附加到元素的特定事件上。 (eg. for text box with class "required" it will attach required() on its onblur) (例如,对于具有“必需”类的文本框,它将在onblur上附加required())

在此处输入图片说明

What is the reason for this error? 此错误的原因是什么?

Edit 编辑

The problem is not in getElementsByClassName() method since it is returning the array of elements with class required as you can see in IE8 watches 问题不在getElementsByClassName()方法中,因为它正在返回具有所需类的元素数组,如您在IE8手表中所见

在此处输入图片说明

Try this solution - I tested it and it worked for your snippet (you have to take the code he wrote in the "Update:" paragraph): 尝试以下解决方案-我对其进行了测试,并且对您的代码段有效(您必须使用他在“ Update:”段中编写的代码):

javascript document.getElementsByClassName compatibility with IE javascript document.getElementsByClassName与IE的兼容性

Looks like getElementsByClassName is not supported in IE8. IE8不支持getElementsByClassName

By the way: This is a perfect example why you should use jQuery - since you won't have issues like this there :) 顺便说一句:这是一个为什么您应该使用jQuery的完美示例-因为那里不会出现这样的问题:)

edit: I tested it like this: 编辑:我像这样测试它:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">

            function getElementsByClassName(node, classname) {
                var a = [];
                var re = new RegExp('(^| )'+classname+'( |$)');
                var els = node.getElementsByTagName("*");
                for(var i=0,j=els.length; i<j; i++)
                    if(re.test(els[i].className))a.push(els[i]);
                return a;
            }

            window.onload = function() {
                var a = getElementsByClassName(document.body,"required");

                for(var i = 0;i < a.length;i++)
                    a[i].onblur = function() { alert("blured")};
            };
        </script>
    </head>
    <body>
       <input class="required" type="text" name="uno" />
       <input class="required" type="text" name="duno" />
    </body>
</html>

getElementsByClassName is not supported in IE8. IE8不支持getElementsByClassName

You can try document.querySelectorAll or document.querySelector . 您可以尝试document.querySelectorAlldocument.querySelector It is more flexible and very comfortable to use. 它更加灵活,使用起来非常舒适。

In your case it would be: 您的情况是:

var a = document.querySelectorAll(".required");

You can use basic css-selectors to get your DOM-Elements such as querySelector("#ID .class") as you would in jQuery. 您可以像使用jQuery一样,使用基本的CSS选择器来获取DOM元素,例如querySelector("#ID .class")

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

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