输入专注于另一个输入

时间:2018-09-12 11:23:56

标签: html css jquery-ui

我尝试使用此UI输入,但是当我单击电子邮件标签区域(100%width电子邮件标签)时,它将专注于名称输入

我尝试使用javascrip并将输入的CSS代码分隔开,但它不起作用,然后我意识到 这是因为position,我该如何解决这个问题?

这是我的代码:

.centered-name {
  width: 50%;
  margin: auto;
}

.centered-email {
  width: 75%;
  margin: auto;
}

.group {
  width: 100%;
  overflow: hidden;
  position: relative;
}

.label {
  position: absolute;
  top: 40px;
  color: #666666;
  font: 400 26px Roboto;
  cursor: text;
  transition: 0.3s ease;
  width: 100%;
  text-align: center;
}

.input {
  display: block;
  width: 100%;
  padding-top: 36px;
  border: none;
  border-radius: 0;
  font-size: 30px;
  transition: .3s ease;
  text-align: center;
}

.input:valid~.label {
  top: 3px;
  font: 400 26px Roboto;
}

.input:focus {
  outline: none;
}

.input:focus~.label {
  top: 3px;
  font: 400 26px Roboto;
}

.input:focus~.bar:before {
  transform: translateX(0);
}

.bar {
  border-bottom: 2px solid #ff5126;
  width: 100%;
  margin-top: 6px;
  transition: .3s ease;
}

.input:valid~.bar {
  border-bottom: 2px solid #3bb873;
}
<div class="centered-name">
  <div class="group">
    <input type="text" minlength="3" dir="rtl" class="input" id="name" required autocomplete="off" />
    <label class="label" for="name">Name</label>
    <div class="bar"></div>
  </div>
</div>

<div class="centered-email">
  <div class="group">
    <input type="email" class="input" id="name" required autocomplete="off" />
    <label class="label" for="name">Email</label>
    <div class="bar"></div>
  </div>
</div>

3 个答案:

答案 0 :(得分:5)

您忘记为emailinput分别分配for="email"label的不同ID。请尝试以下:

.centered-name {
  width: 50%;
  margin: auto;
}

.centered-email {
  width: 75%;
  margin: auto;
}

.group {
  width: 100%;
  overflow: hidden;
  position: relative;
}

.label {
  position: absolute;
  top: 40px;
  color: #666666;
  font: 400 26px Roboto;
  cursor: text;
  transition: 0.3s ease;
  width: 100%;
  text-align: center;
}

.input {
  display: block;
  width: 100%;
  padding-top: 36px;
  border: none;
  border-radius: 0;
  font-size: 30px;
  transition: .3s ease;
  text-align: center;
}

.input:valid~.label {
  top: 3px;
  font: 400 26px Roboto;
}

.input:focus {
  outline: none;
}

.input:focus~.label {
  top: 3px;
  font: 400 26px Roboto;
}

.input:focus~.bar:before {
  transform: translateX(0);
}

.bar {
  border-bottom: 2px solid #ff5126;
  width: 100%;
  margin-top: 6px;
  transition: .3s ease;
}

.input:valid~.bar {
  border-bottom: 2px solid #3bb873;
}
<div class="centered-name">
  <div class="group">
    <input type="text" minlength="3" dir="rtl" class="input" id="name" required autocomplete="off" />
    <label class="label" for="name">Name</label>
    <div class="bar"></div>
  </div>
</div>

<div class="centered-email">
  <div class="group">
    <input type="email" class="input" id="email" required autocomplete="off" />
    <label class="label" for="email">Email</label>
    <div class="bar"></div>
  </div>
</div>

答案 1 :(得分:1)

更改您的输入电子邮件ID及其用于服装的标签

.centered-name {
  width: 50%;
  margin: auto;
}

.centered-email {
  width: 75%;
  margin: auto;
}

.group {
  width: 100%;
  overflow: hidden;
  position: relative;
}

.label {
  position: absolute;
  top: 40px;
  color: #666666;
  font: 400 26px Roboto;
  cursor: text;
  transition: 0.3s ease;
  width: 100%;
  text-align: center;
}

.input {
  display: block;
  width: 100%;
  padding-top: 36px;
  border: none;
  border-radius: 0;
  font-size: 30px;
  transition: .3s ease;
  text-align: center;
}

.input:valid~.label {
  top: 3px;
  font: 400 26px Roboto;
}

.input:focus {
  outline: none;
}

.input:focus~.label {
  top: 3px;
  font: 400 26px Roboto;
}

.input:focus~.bar:before {
  transform: translateX(0);
}

.bar {
  border-bottom: 2px solid #ff5126;
  width: 100%;
  margin-top: 6px;
  transition: .3s ease;
}

.input:valid~.bar {
  border-bottom: 2px solid #3bb873;
}
<div class="centered-name">
  <div class="group">
    <input type="text" minlength="3" dir="rtl" class="input" id="name" required autocomplete="off" />
    <label class="label" for="name">Name</label>
    <div class="bar"></div>
  </div>
</div>

<div class="centered-email">
  <div class="group">
    <input type="email" class="input" id="mail" required autocomplete="off" />
    <label class="label" for="mail">Email</label>
    <div class="bar"></div>
  </div>
</div>

答案 2 :(得分:0)

如果您只需要更改输入的名称以及与之关联的标签for=

.centered-name {
  width: 50%;
  margin: auto;
}

.centered-email {
  width: 75%;
  margin: auto;
}

.group {
  width: 100%;
  overflow: hidden;
  position: relative;
}

.label {
  position: absolute;
  top: 40px;
  color: #666666;
  font: 400 26px Roboto;
  cursor: text;
  transition: 0.3s ease;
  width: 100%;
  text-align: center;
}

.input {
  display: block;
  width: 100%;
  padding-top: 36px;
  border: none;
  border-radius: 0;
  font-size: 30px;
  transition: .3s ease;
  text-align: center;
}

.input:valid~.label {
  top: 3px;
  font: 400 26px Roboto;
}

.input:focus {
  outline: none;
}

.input:focus~.label {
  top: 3px;
  font: 400 26px Roboto;
}

.input:focus~.bar:before {
  transform: translateX(0);
}

.bar {
  border-bottom: 2px solid #ff5126;
  width: 100%;
  margin-top: 6px;
  transition: .3s ease;
}

.input:valid~.bar {
  border-bottom: 2px solid #3bb873;
}
<div class="centered-name">
  <div class="group">
    <input type="text" minlength="3" dir="rtl" class="input" id="name" required autocomplete="off" />
    <label class="label" for="name">Name</label>
    <div class="bar"></div>
  </div>
</div>

<div class="centered-email">
  <div class="group">
    <input type="email" class="input" id="email" required autocomplete="off" />
    <label class="label" for="email">Email</label>
    <div class="bar"></div>
  </div>
</div>