1

我在使用 & 符号时遇到了一些困难。如果我这样使用它:

.form-standard {
  ...
  .form--group {
    &.has-error {
      border-color: $form-error-color;
      color: $form-error-color;

      .form--feedback::before {content: '*';} // <-- here

      &.has-focus {
        box-shadow: inset 0 1px 3px rgba(#000, 0.06), 0 0 5px rgba($form-error-color, 0.7);
        color: $form-error-color;
      }// has-focus

    }// has-error
  }// form--group
}// form-standard

然后我正在实现我想要的,即:

.form-standard .form--group.has-error .form--feedback::before {
  content: '*';
}

但是 linter 抱怨伪元素应该嵌套在它的父类中。我试过这个:

.form-standard {
  ...
  .form--group {    
    .form--feedback {
      clear: left;
      font-size: 12px;
      margin: 0;
      padding: 0;
      padding-left: 5px;

      .has-error & {              //   
        &::before {content: '*';} // produces same output
      }                           //

      &::before {                    //
        .has-error & {content: '*';} // produces same output
      }                              //
    }// form--feedback
  }// form--group
}// form-standard  

但结果不是我想要的:

.has-error .form-standard .form--group .form--feedback::before {
  content: '*';
}

这是 HTML:

<form class="form form-standard" role="form">

  <div class="form--group">
    <label>category</label>
    <input id="category" type="text" name="category">
  </div>

</form>
4

1 回答 1

1

按照 Harry 的建议将伪元素向下移动一级解决了 lint 警告并且根本不需要 & 符号:

&.has-error {
  border-color: $form-error-color;
  color: $form-error-color;

  //.form--feedback::before {content: '*';} // before had a warning
  // after
  .form--feedback {
    &::before {content: '*';}
  }
}
于 2016-11-28T15:45:10.923 回答