简体   繁体   English

当输入为焦点或填充时,则显示div jquery

[英]When input is focus or filled in then show div jquery

I'm trying to get the condition, when input is focus or filled in then show a div on each input element. 我正在尝试获取条件,当输入为焦点或填充时,然后在每个输入元素上显示一个div。

On focus it is working fine, but not able to get this work on filled input. 重点是它工作正常,但无法在填充的输入上完成这项工作。

 $('.field').focus(function() { $('.placeholder-text').hide(); var i = 0; $('.field').each(function() { if ($(this).is(":focus") || $(this).val() > 0) { $($('.placeholder-text')[i]).show(); } i++; }) $(document).bind('focusin.placeholder-text click.placeholder-text', function(e) { if ($(e.target).closest('.placeholder-text, .field').length) return; $(document).unbind('.placeholder-text'); $('.placeholder-text').fadeOut('medium'); }); }); $('.placeholder-text').hide(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="name" class="field" name="name" type="text" placeholder="First Name" /> <div class="placeholder-text" for="name">First Name</div> <input id="lname" class="field" type="text" name="lname" placeholder="Last Name" /> <span class="placeholder-text" for="lname">Last Name</span> 

While this is, of course, possible with JavaScript – and any of its libraries – it's also possible using pure CSS, if you're able to add the required attribute to the <input> elements, and then styling the visibility of the adjacent .placeholder-text element with the :valid psuedo-class: 尽管使用JavaScript及其所有库当然可以做到这一点,但如果能够将required属性添加到<input>元素,然后设计相邻元素的可见性,则也可以使用纯CSS .placeholder-text带有:valid psuedo-class的.placeholder-text元素:

input:focus+.placeholder-text,
input:valid+.placeholder-text {
  opacity: 1;
}

Note that I've used a transition of the element's opacity to adjust the visibility of the .placeholder-text elements to avoid the jarring effect of the sudden appearance/disappearance inherent in using display: none / display: initial (or display: block ). 请注意,我已经使用元素的opacity过渡来调整.placeholder-text元素的可见性,以避免使用display: none / display: initial (or display: block )固有的突然出现/消失的震撼效果。 。

 body { display: grid; grid-template-columns: repeat(2, 1fr); } input+.placeholder-text { opacity: 0; transition: opacity 0.2s linear; } input:focus+.placeholder-text, input:valid+.placeholder-text { opacity: 1; } 
 <input id="name" class="field" name="name" type="text" placeholder="First Name" required /> <div class="placeholder-text" for="name">First Name</div> <input id="lname" class="field" type="text" name="lname" placeholder="Last Name" required /> <span class="placeholder-text" for="lname">Last Name</span> 

It's also possible – with CSS – using the :placeholder-shown pseudo-class; 在CSS中,也可以使用:placeholder-shown伪类。 though this depends on the browser compatibility you require (Internet Explorer, Edge and Opera Mini have no support as I write); 尽管这取决于您所需的浏览器兼容性(在我撰写本文时,Internet Explorer,Edge和Opera Mini不支持); the :placeholder-shown pseudo-class matches an <input> element if it's placeholder value is currently visible: :placeholder-shown伪类与<input>元素匹配,如果它的占位符值当前可见:

 body { display: grid; grid-template-columns: repeat(2, 1fr); } input + .placeholder-text { opacity: 0; transition: opacity 0.2s linear; } input:not(:placeholder-shown) + .placeholder-text { opacity: 1; } 
 <input id="name" class="field" name="name" type="text" placeholder="First Name" /> <div class="placeholder-text" for="name">First Name</div> <input id="lname" class="field" type="text" name="lname" placeholder="Last Name" /> <span class="placeholder-text" for="lname">Last Name</span> 

Or, to simplify the CSS selector and avoid the use of the :not() negation operator: 或者,为了简化CSS选择器并避免使用:not()求反运算符:

 body { display: grid; grid-template-columns: repeat(2, 1fr); } input:placeholder-shown+.placeholder-text { opacity: 0; } input+.placeholder-text { opacity: 1; transition: opacity 0.2s linear; } 
 <input id="name" class="field" name="name" type="text" placeholder="First Name" required /> <div class="placeholder-text" for="name">First Name</div> <input id="lname" class="field" type="text" name="lname" placeholder="Last Name" required /> <span class="placeholder-text" for="lname">Last Name</span> 

References: 参考文献:

  1. use .on instead of .bind, but do not bind on each and every focus like you are now 使用.on而不是.bind,但不要像现在那样绑定每个焦点
  2. bind on focus, blur and on input 绑定焦点,模糊和输入
  3. there is no "for" except for labels 除标签外,没有“用于”

 $(".field").on("input", function() { $(this).next().toggle(this.value != ""); }); $(".field").on("blur", function() { if (this.value == "") $(this).next().fadeOut('medium'); }) $(".field").on("focus", function() { $(this).next().fadeIn('medium'); }) 
 .placeholder-text { display:none } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="name" class="field" name="name" type="text" placeholder="First Name" /><span class="placeholder-text">First Name</span><br/> <input id="lname" class="field" type="text" name="lname" placeholder="Last Name" /><span class="placeholder-text">Last Name</span> 

I have made some changes to your HTML structure and I have given some CSS too. 我对您的HTML结构进行了一些更改,并且也提供了一些CSS。 I have optimized the jQuery code and it is according to your requirements. 我已经优化了jQuery代码,它根据您的要求。 Please have a look. 请看一看。

 $(".first-name #name, .last-name #lname").on("focus", function() { $(this).parent().find(".placeholder-text").show(); }); $(".first-name #name, .last-name #lname").blur("focus", function() { $(this).parent().find(".placeholder-text").hide(); if ($(this).val() == '' || $(this).val() == null) { $(this).parent().find(".placeholder-text").show(); } }); 
 .placeholder-text { display: none; } .flex-box { display: flex; } .first-name, .last-name { display: inline; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="flex-box"> <div class="first-name"> <input id="name" class="field" name="name" type="text" placeholder="First Name" /> <div class="placeholder-text" for="name">First Name</div> </div> <div class="last-name"> <input id="lname" class="field" type="text" name="lname" placeholder="Last Name" /> <span class="placeholder-text" for="lname">Last Name</span> </div> </div> 

I hope this was helpful. 我希望这可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM