【问题标题】:Labels are overriding the records标签覆盖了记录
【发布时间】:2017-06-17 12:32:30
【问题描述】:

一切正常我遇到了一点问题。我有两个字段电子邮件和密码。我的电子邮件 ID 和密码保存在浏览器中。当我点击 URL 时,我会在字段中自动填写电子邮件和密码,但我的占位符会覆盖字段记录。我必须在文本字段下方显示标签。请检查下图。

(function() {
  // trim polyfill : https://developer.mozilla.org/en-      US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
  if (!String.prototype.trim) {
    (function() {
      // Make sure we trim BOM and NBSP
      var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
      String.prototype.trim = function() {
      return this.replace(rtrim, '');
    };
    })();
  }

  [].slice.call(document.querySelectorAll('input.input__field')).forEach(function(inputEl) {
  // in case the input is already filled..
    if (inputEl.value.trim() !== '') {
    classie.add(inputEl.parentNode, 'input--filled');
    }

    // events:
    inputEl.addEventListener('focus', onInputFocus);
    inputEl.addEventListener('blur', onInputBlur);
  });

  function onInputFocus(ev) {
    classie.add(ev.target.parentNode, 'input--filled');
  }

  function onInputBlur(ev) {
    if (ev.target.value.trim() === '') {
      classie.remove(ev.target.parentNode, 'input--filled');
    }
  }
})();
<script src="https://tympanus.net/Development/TextInputEffects/js/classie.js"></script>
<link href="https://tympanus.net/Development/TextInputEffects/css/normalize.css" rel="stylesheet" />
<link href="https://tympanus.net/Development/TextInputEffects/css/set2.css" rel="stylesheet" />
<link href="https://tympanus.net/Development/TextInputEffects/css/demo.css" rel="stylesheet" />

<section class="content bgcolor-4">
  <h2>Ruri</h2>
  <span class="input input--ruri">
    <input class="input__field input__field--ruri" type="email" id="input-26" />
    <label class="input__label input__label--ruri" for="input-26">
      <span class="input__label-content input__label-content--ruri">Email</span>
    </label>
  </span>
  <span class="input input--ruri">
    <input class="input__field input__field--ruri" type="password" id="input-27" />
    <label class="input__label input__label--ruri" for="input-27">
       <span class="input__label-content input__label-content--ruri">Password</span>
    </label>
  </span>
</section>

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

如果您已经自动放置了它们的每个值(假设它不是空字符串),您需要在每个 emailpassword 元素父 span.input--ruri 上添加一个类 input--filled

$(document).ready(function() {
    var oInputEmail = $('#input-26');
    if ($.trim(oInputEmail.val()) !== '') {
       oInputEmail.parent().addClass('input--filled');
    }

    var oInputPass = $('#input-27');
    if ($.trim(oInputPass.val()) !== '') {
       oInputPass.parent().addClass('input--filled');
    }
});

这里有一个 jsfiddle 可以帮助你: http://jsfiddle.net/1Ld08fxf/

希望这对您的情况有所帮助

【讨论】:

  • 感谢您的回复,字段会自动填充,因为凭据保存在浏览器中,当我点击该字段时,电子邮件标签会像演示一样显示在下方。
  • 抱歉延迟回复。我应该在哪里添加类输入--填充?
  • 我检查了你的代码。它仅适用于电子邮件,不适用于密码。
  • 现在我明白了,我将 添加到电子邮件和密码中,并且工作正常。谢谢你的帮助。从我这边投票
  • 没问题。很高兴我能帮上忙。如果您认为该答案对您有帮助,请将此答案标记为“可接受”
猜你喜欢
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多