对于下面的代码,我在IE中遇到了一些问题。传递给函数的第二个参数应该是对单击项目的引用。这在FF和Safari中运行良好,但是当我在IE7中测试它时会出错。 IE似乎得到了元素(如控制台中所示)但每当我尝试用它做任何事情时我都会收到错误:

<!>“;对象不支持此属性或方法<!>”;

感谢您的帮助。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>  
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>
<script type='text/javascript' src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>   
<script language="javascript" type="text/javascript">   
    var Test = {            
        doIt: function(str, btn) {  
            console.log(str);
            console.log(btn);               
            if (btn.hasClassName('red')) {
                console.log('has red');         
            } else {
                console.log('doesn\'t');
            }           
        }   
    };  
</script>   
<a href="#" onClick="Test.doIt('hello', this)" class="red">Test</a> 
</body></html>
有帮助吗?

解决方案

问题是btn不会自动拥有方法hasClassName - 这是一个Prototype扩展。

使用原型扩展功能扩展您的元素,请在顶部添加此行of doIt()

btn = $(btn); // Extends element with Prototype functions.

您的代码应该可以在那里工作。

其他提示

这是因为在使用Prototype时,元素不会在IE中的页面加载时使用Prototype函数自动扩展,它们必须通过$()调用以使用这些方法扩展元素。将其添加到函数顶部,使其在IE中运行。

doIt : function(str, btn) {
    btn = $(btn);
}

编辑:为了澄清,在IE 7(iirc)以下的所有浏览器中,HTML Elements将自动原型化,但在IE <!> lt; = 7中,它们必须通过< =>使用这些原型的功能。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top