最近在IE=7模式下更新IE (11.0.27)后,javasrcipt中存在一些非常奇怪的行为,下面的早期版本脚本按预期工作(包括IE8-IE11.0.24)。
所以,这是由调用Element.getAttributeNode('class')引起的,在对元素进行调用之后,如果您尝试用Element.mergeAttributes方法将它的属性与另一个元素合并,则该元素将抛出错误(未指定的错误)。
演示(仅当devtools/console被关闭时,并且在IE-7模式下使用当前的IE11版本):链接
问:是否有任何办法来避免这种情况,对上述那些人是否有任何替代办法?问题是这两种方法被mootools和jquery选择器大量使用,而我的框架是基于mootools的。因此,在我使用类似于$$('inputclass=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>发布于 2016-02-04 14:34:21
问题似乎在jQuery库中。这个解决方案是由cqrranthuohill3从dataTabes论坛提供的。他提到了Dottoro.com
在jquery.js中,cloneFixAttributes( src,dest )函数约为6092行。取代:
// 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);
}
}
}试试看。
发布于 2016-02-05 15:03:47
在得到一些建议之后,我设法修复了与Element.getAttributeNode (类)相关的问题,因为它占用了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;
}https://stackoverflow.com/questions/34946941
复制相似问题