简体   繁体   English

IE:输入字段的占位符会影响选择框的宽度

[英]IE: Placeholder of input field affect the width of select box

<form>
 <input type="text" placeholder="some text">
 <select>
  <option>op1</option>
  <option>op2</option>
  <option>op3</option>
 </select>
</form>

The first I click on select box. 第一个我点击选择框。 It's work, width of select area fit to select control. 这是工作,选择区域的宽度适合选择控制。 But If I click on text field then click to select box, the select area look larger. 但是,如果单击文本字段然后单击以选中框,则选择区域看起来更大。 If I remove placeholder attribute for input field, it work properly. 如果我删除输入字段的占位符属性,它可以正常工作。 I think this is the issue of internet explorer placeholder. 我认为这是Internet Explorer占位符的问题。 https://jsfiddle.net/22v1og48/ Any ideas for this issue? https://jsfiddle.net/22v1og48/关于这个问题的任何想法? How to still keep placeholder but select box still work properly? 如何保持占位符但选择框仍然正常工作?

The only way I was able to fix this issue was to remove the placeholder on focus and set it back on blur: 我能解决这个问题的唯一方法是删除焦点上的占位符并将其设置为模糊:

 $('#a1') .data('placeholder', $('#a1').attr('placeholder')) .focus(function() { $(this).attr('placeholder', ''); }) .blur(function() { $(this).attr('placeholder', $(this).data('placeholder')); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input id="a1" type="text" placeholder="some text"><select> <option>op1</option> <option>op2</option> <option>op3</option> </select> </form> 

Note that in IE the placeholder disappear when you focus, but this is no how it works in other browsers (firefox/chrome), where the placeholder disappear when you start typing. 请注意,在IE中, placeholder在您聚焦时消失,但这并非在其他浏览器(firefox / chrome)中的工作原理,当您开始键入时占位符消失。 This code will change this behavior, but will make the select keep it's original size. 此代码将更改此行为,但会使select保持原始大小。

Also note that I used jQuery for the example, but you can do exactly the same without jQuery (it's was only faster to write this example, thats all). 还要注意我在这个例子中使用了jQuery,但你可以在没有jQuery的情况下完全相同(编写这个例子的速度更快,那就是全部)。

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

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