是否可以在<input placeholder =“Name *”/>中设置字符串的一部分的样式

时间:2017-03-23 14:54:21

标签: jquery css forms

我正在使用ContactForm7进行wordpress。它正在为我生成形式。我想在输入标签中设置星号的样式,但由于CF7的可编辑区域是短代码,我认为我不能这样做。如果我的代码输出如下所示:

<input type="text" name="email" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Email*">

有没有办法(使用css)或(jquery,如果需要)从placeholder="Name*"中选择星号,这样我就可以改变颜色而不影响“占位符”这个词的颜色。

2 个答案:

答案 0 :(得分:0)

您可以定位输入的容器并为其添加伪元素。你需要非常精确地定位它......

注意此代码段假设容器是表单元素,您可以更改占位符文本以删除原始星号。

&#13;
&#13;
form {
  position: relative;
}

form::after {
  content: "*";
  color: red;
  position: absolute;
  left: 35px;
  opacity: .8;
}
&#13;
<form action="">
  <input type="text" name="email" value="" size="40" placeholder="Email">
</form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

以下是我的评论中的建议。

<强> HTML

<input type="text" name="email" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Email" />

<强> CSS

.placeholder-label {
  display: inline-block;
  position: absolute;
  color: grey;
  padding: 2px 3px;
}

.placeholder-label em {
  color: red;
}

<强>的JavaScript

$(function() {
  function makeRequiredLabel($o) {
    $o.each(function(ind, el) {
      var ph = $(el).attr("placeholder");
      $(el).attr("placeholder", "");
      var $label = $("<label>", {
        class: "placeholder-label"
      }).html(ph + " <em>*</em>");
      $label.insertAfter($(el)).css({
        top: $(el).position().top + "px",
        left: $(el).position().left + "px"
      });
    });
  }

  makeRequiredLabel($(".wpcf7-validates-as-required"));
  $(".wpcf7-validates-as-required").on("click focus blur focusOut", function(e){
    var target = $(this).next(".placeholder-label");
    switch(e.type){
      case "click":
      case "focus":
         target.hide();
         break;
      case "blur":
      case "focusOut":
         if($(this).val() === ""){
           target.show();
         }
    }
  });
});

如果您添加jQuery UI库,则可以将其设为自己的小部件。

示例:https://jsfiddle.net/Twisty/aLvypLv8/

<强>的JavaScript

$(function() {
  $.widget("custom.requiredLabel", {
    options: {
      labelColor: "grey",
      labelFont: "Arial",
      starColor: "red"
    },
    _create: function() {
      this.placeholder = this.element.attr("placeholder");
      this.element.attr("placeholder", "");
      this._on(this.element, {
        click: "hide",
        focus: "hide",
        blur: "show",
        focusOut: "show"
      });
      this._createRequired();
    },
    _createRequired: function() {
      this.label = $("<label>", {
          class: "ui-require-label"
        }).css({
          display: "inline-block",
          position: "absolute",
          color: this.options.labelColor,
          "font-family": this.options.labelFont,
          padding: "2px 3px"
        })
        .append(this.placeholder, " ", $("<em>", {
          style: "color: " + this.options.starColor + ";"
        }).html("*"))
        .insertAfter(this.element).css({
          top: this.element.position().top + "px",
          left: this.element.position().left + "px"
        });
      this.element.focus(function() {
        this.label.hide();
      });
      this.element.blur(function() {
        if (this.element.val() == "") {
          this.lebal.show();
        }
      });
    },
    _setOptions: function() {
      this.superApply(arguments);
    },
    _setOption: function(key, val) {
      if (/labelColor|labelFont|starColor/.test(key)) {
        return;
      } else {
        this._super(key, value);
      }
    },
    _refresh: function() {
      this.label.css({
        color: this.options.labelColor,
        "font-family": this.options.labelFont,
      });
      this.label.find("em").css("color", this.options.starColor);
    },
    hide: function(event) {
      this.label.hide();
    },
    show: function(event) {
      if (this.element.val() === "") {
        this.label.show();
      }
    }
  });

  $(".wpcf7-validates-as-required").requiredLabel({
    labelColor: "#cfcfcf",
    starColor: "#ff9999"
  });
});