如果用户开始输入,则显示元素

时间:2017-06-06 10:52:11

标签: jquery

我正在尝试在满足以下条件时显示元素: 1.输入是焦点 2.输入有1个或多个字符。

到目前为止我的jquery代码(不起作用)是这个......

$('input').click(function() {
  if (($(this).is(':focus')) && ($(this).val().length > 1)) {
    $(this).siblings('.show').fadeIn();
  }
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
  font-size: 16px;
  color: #5c5c5c;
  line-height: 22px;
}

main {
  max-width: 600px;
  margin: 20px auto;
  padding: 16px;
}

.group {
  margin-bottom: 16px;
  position: relative;
}

label {
  color: #393939;
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
}

input,
textarea {
  font-family: sans-serif;
  font-size: 16px;
  color: #5c5c5c;
  border-radius: 2px;
  border: 1px solid #ccc;
  width: 100%;
  padding: 12px;
  margin-bottom: 4px;
  -webkit-appearance: none;
  outline: none;
  transition: all 0.4s ease;
}

input:not(textarea) {
  height: 48px;
}

input:focus,
textarea:focus {
  box-shadow: inset 0 0 0 2px rgba(74, 168, 81, 1);
}

::placeholder {
  font-family: sans-serif;
  color: #bbb;
}

.hint {
  font-size: 14px;
  line-height: 20px;
  color: #727272;
  position: relative;
  top: -5px;
  opacity: 0;
  z-index: -1;
  transition: all 0.2s ease;
}

input:focus+.hint {
  opacity: 1;
  top: 0;
}

.show {
  font-size: 14px;
  line-height: 20px;
  display: none;
  position: absolute;
  top: 44px;
  right: 14px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<main>
  <div class="group">
    <label for="email">Email</label>
    <input type="text" id="email" placeholder="e.g. jamesgreen@gmail.com">
  </div>

  <div class="group">
    <label for="password">Password</label>
    <input type="password" id="password">
    <p class="hint">Password must have at least 8 characters, a lower case letter, an uppercase letter and a number</p>
    <span class="show">SHOW</span>
  </div>
</main>

不知道我哪里出错了,这里有codepen

的链接

1 个答案:

答案 0 :(得分:2)

您可以使用input事件代替click事件和focus选择器:

&#13;
&#13;
$('input').on('input', function(){
    if ($(this).val().length > 1) {
		$(this).siblings('.show').fadeIn();
	} else {
          $(this).siblings('.show').fadeOut();
    }
});
			
&#13;
* {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}


body {
	font-family: sans-serif;
	font-size: 16px;
	color: #5c5c5c;
	line-height: 22px;
}

main {
	max-width: 600px;
	margin: 20px auto;
	padding: 16px;
}

.group {
	margin-bottom: 16px;
	position: relative;
}

label {
	color: #393939;
	display: block;
	margin-bottom: 8px;
	font-weight: bold;
}

input,
textarea {
	font-family: sans-serif;
	font-size: 16px;
	color: #5c5c5c;
	border-radius: 2px;
	border: 1px solid #ccc;
	width: 100%;
	padding: 12px;
	margin-bottom: 4px;
	-webkit-appearance: none;
	outline: none;	
	transition: all 0.4s ease;
}

input:not(textarea){
	height: 48px;
}

input:focus,
textarea:focus {
	box-shadow: inset 0 0 0 2px rgba(74, 168, 81, 1);
}

::placeholder {
	font-family: sans-serif;
	color: #bbb;
}

.hint {
	font-size: 14px;
	line-height: 20px;
	color: #727272;
	position: relative;
	top: -5px;
	opacity: 0;
	z-index: -1;
	transition: all 0.2s ease;
}

input:focus + .hint {
	opacity: 1;
	top: 0;
}

.show {
	font-size: 14px;
	line-height: 20px;
	display: none;
	position: absolute;
    top: 44px;
    right: 14px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<main>
  <div class="group">
		<label for="email">Email</label>
	    <input type="text" id="email" placeholder="e.g. jamesgreen@gmail.com">
	</div>

	<div class="group">
		<label for="password">Password</label>
	    <input type="password" id="password">
	    <p class="hint">Password must have at least 8 characters, a lower case letter, an uppercase letter and a number</p>
	    <span class="show">SHOW</span>
	</div>
</main>
&#13;
&#13;
&#13;

相关问题