ASP中的JavaScript在IE中的表现与FireFox不同

JavaScript in ASP behaving different in IE as to FireFox

本文关键字:FireFox 不同 中的 JavaScript IE ASP      更新时间:2023-12-13

我有一个ASP页面,其中JavaScript在IE10中的表现(正确)与在FireFox(最新版本)中的表现不同。

我有一个随机颜色生成器如下:

// Function to generate random colours
function rgbcolors() {
  // rgb string generation
  var col = "rgb("
  + Math.floor(Math.random() * 255) + ","
  + Math.floor(Math.random() * 255) + ","
  + Math.floor(Math.random() * 255) + ")";
  //change the text color with the new random color
  return col;
}

然后我使用一个变量来存储随机颜色:

var strColor = rgbcolors();

现在,当我在其他地方使用这个颜色变量时,它似乎在FireFox中重新生成了一个随机颜色,而不是存储在变量中的颜色:

strHTML += '<td align="center"><font color="' + strColor + '">'

然后这里的颜色不同:

var pieData = [
  {
    value: intUsed,
    color: strColor
  },

奇怪的是,IE10能100%正常工作。

根据<font>标签的MDN文档,您会遇到以下问题:

  1. <font>在HTML 4.01中被弃用,在HTML 5中被弃用(尽管浏览器将保留对它的各种形式的支持)。但是,没有任何理由编写使用它的新代码
  2. 颜色属性采用命名颜色或#RRGGBB格式。没有提到rgb(x,y,z)格式支持<font color=xxx>属性

我建议您改用style.color


此外,请记住,当您有这样的数据结构时:

var pieData = [
  {
    value: intUsed,
    color: strColor
  }

这将捕获strColor字符串变量的当前值。如果您随后更改strColor的值,您的pieData数据结构中仍将有strColor的旧值。