innerHTML适用于IE和Firefox,但不适用于Chrome [英] innerHTML works in IE and Firefox, but not Chrome

查看:122
本文介绍了innerHTML适用于IE和Firefox,但不适用于Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据不会显示在Chrome中,除非我在Chrome中打开IE标签转到该网站然后将其关闭回Chrome(抱歉,如果这没有多大意义)。

The data will not display in Chrome, unless i open an IE tab in Chrome go to the site then close it back to Chrome (sorry, if that doesn't make much sense).

window.onload = function() {
    var url = "http://----.freeiz.com/gbSales/sales.json";
    var request = new XMLHttpRequest();
    request.open("GET", url);
    request.onload = function () {
        if (request.status == 200) {
            updateSales(request.responseText);
        }
    };
    request.send(null);
}
function updateSales(responseText) { 
    var salesDiv = document.getElementById("sales");
    salesDiv.innerHTML = responseText;
}

我刚开始学习JavaScript所以我真的不太了解它。

Im just starting to learn JavaScript so I really don't know much about it.

推荐答案

你应该使用一些现代的Javascript库。它可以防止浏览器之间的许多细微差别。我喜欢 jQuery

You should use some modern Javascript library. It guards you from many of those small differences between browsers. I like jQuery.

所以,使用jquery你的代码

So, with jquery your code

window.onload = function() {
  var url = "http://----.freeiz.com/gbSales/sales.json";
  var request = new XMLHttpRequest();
  request.open("GET", url);
  request.onload = function () {
    if (request.status == 200) {
      updateSales(request.responseText);
    }
  };
  request.send(null);
}
function updateSales(responseText) { 
  var salesDiv = document.getElementById("sales");
  salesDiv.innerHTML = responseText;
}

变为

$(document).load(function() {
  var url = "http://----.freeiz.com/gbSales/sales.json";

  $.get(url, {}, function(data) {
    $('#sales').html(data);
  });
});

更短,更清洁,适用于所有浏览器!

Shorter, cleaner and works in all browsers!

这篇关于innerHTML适用于IE和Firefox,但不适用于Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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