javascript - 在除 IE 之外的所有浏览器上都能正常工作*

标签 javascript jquery internet-explorer-8

我试图解决这个问题,但没有成功..在 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/

相关文章:

javascript - 单击确认添加复选框长度

internet-explorer-8 - AngularUI - 兼容的浏览器

jquery - IE 8 及以下版本不会触发点击显示为 : none; Any workaround? 的元素

javascript - 根据 highchart 中的轴,将鼠标悬停在条形图上突出显示相应的 y 轴

javascript - 使用 jQuery 获取具有数组类型名称的文本字段的值

javascript - 如何使用 YouTube iframe 获得更精确的当前、视频和开始时间?

JavaScript/Ajax/Json : Sending objects and arrays

php - 如何使用 jquery ajaxify zend_pagination 结果(已经使用 partialoop)

facebook - "FB is undefined"IE8错误

javascript - 我如何根据索引组合两个数组。