简体   繁体   English

为什么此代码在FireFox和IE中不能使用,而在Chrome中不能使用?

[英]Why Doesn't This Code Work in FireFox and IE but does in Chrome?

I am currently using this code: 我目前正在使用此代码:

Code can be seen here - http://jsbin.com/ESOdELU/1/edit 可以在这里看到代码-http://jsbin.com/ESOdELU/1/edit

var wordRandomizer = {
    run: function (targetElem) {
        var markup = this.createMarkup();
        targetElem.appendChild(markup);
    },
    createMarkup: function () {
        var that = this;
        var frag = document.createDocumentFragment();
        this.elem = document.createElement('span');
        var button = document.createElement('button');
        button.innerText = 'Change Item';
        button.addEventListener('click', function () {
            that.changeItem();
        });
        frag.appendChild(this.elem);
        frag.appendChild(button);
        return frag;
    },
    changeItem: function () {
        var rand = this.getRandInt(1, this.items.length) - 1;
        console.log(rand);
        this.elem.innerText = this.items[rand];
    },
    getRandInt: function (min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    },
    items: ['itemA', 'itemB', 'itemC', 'itemD']
};
wordRandomizer.run(document.body);

The code works fine in Chrome but in FireFox the button shows very small and doesn't work and in IE the code doesn't work at all. 该代码在Chrome中工作正常,但在FireFox中,按钮显示的很小,无法正常工作;在IE中,该代码根本无法正常工作。

The problem is here: 问题在这里:

button.innerText = 'Change Item';

innerText is not a valid DOM property. innerText不是有效的DOM属性。 It may be supported by some browsers, but it's non-standard, and should not be used. 某些浏览器可能支持它,但是它是非标准的,因此不应使用。

You can replace innerText with either: 您可以将innerText替换为:

  • textContent (which is the direct standards-compliant equvalent), textContent (这是直接符合标准的等效项),

or with 或搭配

  • innerHTML (which is a bit different, but will be identical in your example, and is compatible with older browsers) innerHTML (有点不同,但是在您的示例中将是相同的,并且与旧版浏览器兼容)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在IE和Chrome中运行的代码在Firefox中不起作用 - Code working in IE and Chrome doesn't work in Firefox JavaScript代码在chrome上有效,在IE和Firefox上无效 - JavaScript code works on chrome, doesn't work on IE and firefox JS正则表达式代码不适用于Firefox,但适用于Chrome和IE - JS regex code doesn't work with firefox, but works on chrome and IE jQuery UI droppable在IE和Firefox上不起作用(但在Chrome上有效) - jQuery UI droppable doesn't work on IE and Firefox (but does on Chrome) 为什么JavaScript不适用于Firefox / Chrome但适用于IE? - Why doesn't the JavaScript work for Firefox/Chrome but for IE? 为什么此jQuery代码在Chrome和Firefox中能正常工作,而在IE9上却不能工作? - Why does this jQuery code work fine in Chrome and Firefox, but not IE9? 为什么这适用于 IE,但不适用于 Firefox 或 Chrome? - Why does this work for IE but not for Firefox or Chrome? 为什么此功能适用于Firefox,但不适用于Chrome或IE? - Why does this work with Firefox but not Chrome or IE? 为什么这个JS函数有问题? Firefox可以工作,但Chrome不能工作 - Why is wrong with this JS function? Firefox does work but Chrome doesn't 为什么这个代码不能在IE11,Chrome,Firefox中运行,但是可以在IE8中运行? - Why won't this code work in IE11, Chrome, Firefox, but will work in IE8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM