使用elem.dataset与IE和JSFiddle的问题 [英] Problem using elem.dataset with IE and JSFiddle

查看:103
本文介绍了使用elem.dataset与IE和JSFiddle的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我在Chrome上创建的这个JSFiddle中,我发现它无法在IE上工作(我正在使用IE9)。任何理由: http://jsfiddle.net/ZSB67/

In this JSFiddle I created on Chrome, I find that it's unable to work on IE (I'm using IE9). Any reason as to this: http://jsfiddle.net/ZSB67/.

var backImage = [
    "http://alm7.wikispaces.com/file/view/RedBackground.bmp/144018347/RedBackground.bmp",
    "http://www.time2man-up.com/wp-content/uploads/2011/07/black-background.jpg",
    "http://1.bp.blogspot.com/--GorNQoEUxg/TfWPyckVeMI/AAAAAAAAAHk/0208KqQf3ds/s1600/yellow_background.jpg",
    ""
    ];

function changeBGImage(whichImage) {
    if (document.body) {
        document.body.style.background = "url(\"" + backImage[whichImage] + "\")";
    }
}
var buttons = document.querySelectorAll('.bg_swap'),
    button;

for (var i = 0; i < buttons.length; i++) {
    button = buttons[i];
    button.onclick = function() {
        changeBGImage(this.dataset.index);
    };
}


推荐答案

IE不支持 elem.dataset 。您需要明确获取属性: http://jsfiddle.net/ZSB67/1/

IE does not support elem.dataset. You'd need to explicitly get the attribute: http://jsfiddle.net/ZSB67/1/.

changeBGImage(this.getAttribute('data-index'));

将来,您可能需要按F12并查看控制台是否有错误,因为它说了什么导致这个问题。

In the future, you might want pressing F12 and looking at the console for errors, since it said what was causing the problem here.

这篇关于使用elem.dataset与IE和JSFiddle的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆