【问题标题】:IE8 slice call issue - Array.prototype.slice - 'this' is not a Javascript objectIE8 切片调用问题 - Array.prototype.slice - 'this' 不是 Javascript 对象
【发布时间】:2014-10-19 22:25:59
【问题描述】:

这是一个比之前提出的问题更复杂的问题,尝试使用之前给出的响应,但它不起作用。

这是代码

(function () {
    function init() {
        var speed = 330,
            easing = mina.backout;

       [].slice.call(document.querySelectorAll('.grid > a')).forEach(function (el) {
            var s = Snap(el.querySelector('svg')), path = s.select('path'),
                pathConfig = {
                    from: path.attr('d'),
                    to: el.getAttribute('data-path-hover')
                };

            el.addEventListener('mouseenter', function () {
                path.animate({ 'path': pathConfig.to }, speed, easing);
            });

            el.addEventListener('mouseleave', function () {
                path.animate({ 'path': pathConfig.from }, speed, easing);
            });
        });
    }

    init();

})();

【问题讨论】:

  • Array.prototype.slice.call( document.querySelectorAll('.grid > a') )
  • 不幸的是,这不起作用 bencripps。它仍然抛出同样的错误。看看这个截图。 webpagescreenshot.info/i3/53fcb7f1d10883-60451702
  • 你能创建一个jsfiddle吗?
  • jsfiddle 还不够。在 js/jquery-func.js 中查看 link。在 IE 8 或 IE 8 模式下检查。

标签: javascript arrays internet-explorer-8 call slice


【解决方案1】:

只要选择器 ('.grid > a') 符合 CSS2,这应该可以正常工作。因为querySelectorAllonly supports css2 IE8 中的选择器。

而且你不需要调用 slice 方法,可以像这样直接使用 for each

   [].forEach.call(document.querySelectorAll('.grid > a'), function (el) {
        var s = Snap(el.querySelector('svg')), path = s.select('path'),
            pathConfig = {
                from: path.attr('d'),
                to: el.getAttribute('data-path-hover')
            };

        el.addEventListener('mouseenter', function () {
            path.animate({ 'path': pathConfig.to }, speed, easing);
        });

        el.addEventListener('mouseleave', function () {
            path.animate({ 'path': pathConfig.from }, speed, easing);
        });
    });

更新 - [].forEachcompatible with >= IE9

var nodeArray = [].slice.call(document.querySelectorAll('.grid > a'));

for (var i = 0; i < nodeArray.length; i++) {
    var el = nodeArray[i];
    var s = Snap(el.querySelector('svg')),
        path = s.select('path'),
        pathConfig = {
            from: path.attr('d'),
            to: el.getAttribute('data-path-hover')
        };

    el.addEventListener('mouseenter', function () {
        path.animate({
            'path': pathConfig.to
        }, speed, easing);
    });

    el.addEventListener('mouseleave', function () {
        path.animate({
            'path': pathConfig.from
        }, speed, easing);
    });
}

【讨论】:

  • 这会产生不同的错误。 “无法获取未定义或空引用的属性‘调用’link
  • 哎呀,对不起,我忘了提到forEach是在IE9中添加的,请看我的更新
  • 感谢您的持续帮助,但此更新后的代码会在第一行显示 var nodeArray = [].slice.call(document.querySelectorAll('.grid &gt; a')); 看到原始链接 => link
猜你喜欢
  • 2012-11-03
  • 1970-01-01
  • 2012-09-13
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多