IE11错误 - 调用javascript getAttributeNode方法制动mergeAttributes

时间:2016-01-22 12:38:16

标签: javascript internet-explorer-11

在IE = 7模式的IE(11.0.27)最近更新后,javasrcipt中有一些非常奇怪的行为,在以前的版本中,下面的脚本按预期工作(包括IE8-IE11.0.24)。

所以它是由调用Element.getAttributeNode(' ')引起的,在对元素进行此调用之后元素将抛出错误(未指定的错误)如果您尝试使用Element.mergeAttributes方法将其属性与另一个元素合并。

演示(仅当devtools / console启用关闭并且在IE-7模式下使用当前IE11版本时才会发生):LINK

问题:有没有办法避免这种情况,是否有上述方法的替代方法?问题是mootools和jquery选择器大量使用这两种方法,我的框架基于mootools。所以在我使用看起来像$$的选择器('输入[class = randomclass]')之后,所有输入都将被破坏,并且我将无法克隆它们(mergeAttributes)。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<html>

<head>

    <script type='text/javascript'>
        window.onload = function() {
            org = document.getElementById('o');
            cln = document.getElementById('c');

            btn1 = function() {
                cln.mergeAttributes(org);
                alert(cln.className)
            }

            btn2 = function() {
                org.getAttributeNode('class');
                try {
                    cln.mergeAttributes(org);
                } catch (e) {
                    alert(e.description)
                }
            }

        }
    </script>
</head>

<body>


<div id='placeHolder'>
    Original:
    <input type='text' class='original' id='o'></input>
    <br> Clone:
    <input type='text' class='clone' id='c'></input>
    <br>
</div>

    <input type='button' value='mergeAttributes without "getAttributesNode" ' onclick='btn1()'>
    <br>
    <input type='button' value='mergeAttributes with "getAttributesNode" ' onclick='btn2()'>
    <br>

</body>

</html>

2 个答案:

答案 0 :(得分:1)

似乎问题出在jQuery库中。此解决方案由dataTabes论坛的cqrranthuohill3提供。他参考了Dottoro.com

在关于第6092行的cloneFixAttributes(src,dest)函数的jquery.js中。替换:

// mergeAttributes, in contrast, only merges back on the
// original attributes, not the events
if ( dest.mergeAttributes ) {
    dest.mergeAttributes( src );
}

使用:

   // mergeAttributes, in contrast, only merges back on the
   // original attributes, not the events
    try{
        if ( dest.mergeAttributes ) {
            dest.mergeAttributes( src );
        }
    } catch (exception) {
        for (var i=0; i < src.attributes.length; i++) {
            var attr = src.attributes[i];
            var attrName = attr.name.toLowerCase ();
            if (attrName != "id" && attrName != "name") {
                dest.setAttribute (attr.name, attr.value);
            }
        }
    }

放手一搏。

答案 1 :(得分:0)

经过一些建议后,我设法修复与Element.getAttributeNode(&#39; class&#39;)相关的问题,因为它正在代理Element.mergeAttributes(src),我不得不在mootools中替换Element.getAttributeNode的所有实例使用自定义功能。

替换默认Element.getAttributeNode的函数:

getAttributeNode_M = function(el, attr) {
    var AllAttribues = el.attributes;
    for (var i = 0; i < AllAttribues.length; i++) {
        if (el.attributes[i].name.toLowerCase() == attr.toLowerCase()) {
            return el.attributes[i];
        }
    }
    return false;
}