我试图解决这个问题,但没有成功..在 IE8 中遇到问题..它一直说 JSObject 是预期的,但似乎无法弄清楚问题是什么,因为它在其他地方都工作正常..
jQuery(document).ready(function($) {
console.log('here');
// Profile Selector
// Get all the big profile elements listed above.
// Transform the result of `getElementsByTagName` from a NodeList
// to an Array. Just good practice, really.
var profileWrapper = document.getElementById('profileWrapper');
var bigElements = Array.prototype.slice.call(
profileWrapper.getElementsByTagName('div')
);
// Find an element in the `bigElements` array above that
// has a className that contains the `id` argument.
function selectBigElement( id ) {
// loop thru all the elements in `bigElements`, and...
for( var i = 0; i < bigElements.length; ++i ) {
// ... if the current element's className contains the
// query string argument (`id`), then show it...
if( ~bigElements[i].className.indexOf( id ) ) {
bigElements[i].style.display = 'block';
}
// ... Otherwise, hide it.
else {
bigElements[i].style.display = 'none';
}
}
};
$('.mini_profile').mouseover(function(event) {
selectBigElement(this.id);
});
selectBigElement( 179 );
});
最佳答案
通过查看您的代码,您使用了一些不受支持的方法:
Array.prototype.slice.call(profileWrapper.getElementsByTagName('div'));
这会尝试将 NodeList
转换为 Array
。这在 IE8 中不受支持,因为它不支持 NodeList 作为宿主对象。
对于常规的 for
循环,您不需要这样做,因为您可以像数组一样循环遍历 NodeList(不包括 Array.prototype.forEach()
) ,只需删除 Array.prototype.slice.call() 方法并继续使用:
var bigElements = profileWrapper.getElementsByTagName('div');
但是,您正在尝试使用 indexOf
,它是 ECMAScript 5,也是一个 Array.prototype
方法。要获得 IE8 支持,您需要 Polyfill 这两个实现或编写一些包装函数以获得相同的结果。
关于javascript - 在除 IE 之外的所有浏览器上都能正常工作*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21401831/