【问题标题】:why span is not defined in function [duplicate]为什么函数中没有定义 span [重复]
【发布时间】:2022-11-30 20:21:52
【问题描述】:

我在 'showBall' 函数外创建了一个 'color Change' 函数,我想在 'showBall' 函数内使用 'color change' 函数,但未定义 span ..

function colorChange(standard, color) {
  if (standard > 10) {
    span.style.color = color;
  }
}
const showBall = (number, $target) => {
  const span = document.createElement("span");
  span.className = "ball";
  span.innerHTML = lotto[number];
  colorChange(lotto[number], "red");

  $target.append(span);
};

【问题讨论】:

  • 您要查找的术语是:范围

标签: javascript


【解决方案1】:

它未定义,因为您尚未在该范围内定义它。您应该将 spanshowBall 函数传递给 colorChange 作为参数:

function colorChange(standard, color, span) {
  if (standard > 10) {
    span.style.color = color;
  }
}
const showBall = (number, $target) => {
  const span = document.createElement("span");
  span.className = "ball";
  span.innerHTML = lotto[number];
  colorChange(lotto[number], "red", span);

  $target.append(span);
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2016-09-19
    相关资源
    最近更新 更多