Function addClassIf

条件によってクラスを設定するかどうかを決める

const condition = Math.random() > 0.5;
addClassIf(elm, condition, "active");
//
// "active" というクラスがついていないliに "notActive"というクラスを付与する
addClassIf(qsa("li"), (elm) => !hasClass(elm, "active"), "notActive");
  • Parameters

    • target: NodeList | SingleOrArray<StylableElement>
    • condition: boolean | BooleanFunc

      条件。関数でも渡せる

    • token: string

      クラス名

    • alt: string = ""

      conditionfalse だった場合に代わりに設定するクラス名

    • remove: boolean = true

      conditionfalse だった場合に token を外すか

    Returns void