为什么Javascript fontcolor方法不适合我?

时间:2017-05-04 09:48:43

标签: javascript

出于某种原因,倒数计时器标题下方的文字并未将颜色变为红色。出于某种原因,每当我在字符串变量上调用fontcolor()方法时,它都会忽略方法调用并打印出其默认字体颜色。我该如何解决这个问题?



org.hsqldb.jdbc.JDBCConnection




2 个答案:

答案 0 :(得分:0)

您需要存储fontcolor()

的返回值

fontcolor()会返回<font>标记

中嵌入的字符串

&#13;
&#13;
<body>

  <div id="worked"></div>
  <h1 style="text-align: center;"><span style="color: #ff0000;"><strong>Offer Ends In:</strong></span></h1>
  <h1 id="time" style="text-align: center;">&nbsp;</h1>

</body>

<script>
  var handler = function() {
    if (--sec < 0) {
      sec = 59;
      if (--min < 0) {
        min = 0;
        sec = 0;
      }
    }
    var min1 = "0" + min + "m";
    var min2 = min + "m";
    var sec1 = "0" + sec + "s";
    var sec2 = sec + "s";
    var col = ":";
    min1 = min1.fontcolor("red");
    min2 = min2.fontcolor("red");
    sec1 = sec1.fontcolor("red");
    sec2 = sec2.fontcolor("red");
    col = col.fontcolor("red");
    document.getElementById("time").innerHTML = (min < 10 ? min1 : min2) + col + (sec < 10 ? sec1 : sec2);
  };
  var sec = 0;
  var min = 15;
  handler();
  setInterval(handler, 1000);
</script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

仅在将其更新为HTML时应用fontcolor,或者按照@Weedoze的建议保存结果。

col.fontcolor("red")不会更新col,但会返回col中包含<font>值的新字符串。返回的值未在代码中保存或使用。

var col = ":".fontcolor("red");
document.getElementById("time").innerHTML = (min < 10 ? min1.fontcolor("red") : min2.fontcolor("red")) + col + (sec < 10 ? sec1.fontcolor("red") : sec2.fontcolor("red"));

&#13;
&#13;
var handler = function() {
  if (--sec < 0) {
    sec = 59;
    if (--min < 0) {
      min = 0;
      sec = 0;
    }
  }
  var min1 = "0" + min + "m";
  var min2 = min + "m";
  var sec1 = "0" + sec + "s";
  var sec2 = sec + "s";
  var col = ":".fontcolor("red");

  document.getElementById("time").innerHTML = (min < 10 ? min1.fontcolor("red") : min2.fontcolor("red")) + col + (sec < 10 ? sec1.fontcolor("red") : sec2.fontcolor("red"));
};
var sec = 0;
var min = 15;
handler();
setInterval(handler, 1000);
&#13;
<div id="worked"></div>
<h1 style="text-align: center;"><span style="color: #ff0000;"><strong>Offer Ends In:</strong></span></h1>
<h1 id="time" style="text-align: center;">&nbsp;</h1>
&#13;
&#13;
&#13;