【问题标题】:Is it possible to style a portion of a string inside <input placeholder="Name*">是否可以在 <input placeholder="Name*"> 中设置字符串的一部分
【发布时间】:2017-03-23 14:54:21
【问题描述】:

我将 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*" 中只选择星号,这样我就可以在不影响“占位符”一词的颜色的情况下更改颜色?

【问题讨论】:

  • 我知道您可以设置占位符文本的样式,但我不知道您是否可以设置其中的一部分,您可能需要自己制作占位符文本。如果我是你,我不会在占位符中使用星号,而是将它放在输入旁边
  • 没有简单的方法来做到这一点。您可以创建一个&lt;label&gt; 元素,格式化其中的文本,然后将其浮动在&lt;input&gt; 之上以制作一个伪占位符。

标签: jquery css forms


【解决方案1】:

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

注意此 sn-p 假定容器是表单元素,并且您可以更改占位符文本以删除原始星号。

form {
  position: relative;
}

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

【讨论】:

    【解决方案2】:

    这是我评论中的建议。

    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"
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 2014-01-31
      • 2021-05-15
      • 2021-06-04
      相关资源
      最近更新 更多