简体   繁体   English

CSS浮动元素错误

[英]css floating elements error

I have a small problem about div and input floating: 我有一个关于div和输入浮动的小问题:

在此处输入图片说明

This is my problem, with a jQuery script when you wrote in the input box and press enter, a div element will be added, but if we wrote more then 4 element, the input box remain in the first line and the element go down. 这是我的问题,当您在输入框中编写并按Enter时使用jQuery脚本时,将添加一个div元素,但是如果编写的元素多于4个,则输入框将保留在第一行,而该元素将下降。 Can anyone could help me? 谁能帮助我?

 div.box { width: 300px; height: 200px; border: 1px solid #ddd; padding: 5px; } div.box>div.element { background-color: #00B5B5; display: inline-block; color: #fff; font-size: 11px; text-transform: uppercase; padding: 2px 8px 2px 8px; border-radius: 5px; line-height: normal; cursor: pointer; margin-right: 4px; margin-bottom: 4px; float: left; } div.box>input#group-input { height: 11px; /*border: none;*/ font-size: 12px; outline: none; vertical-align: top; width: 8px; } 
 <div class="box" id="box-ins-group"> <div class="element" id="1">prova</div> <input type="text" id="group-input"> </div> 

I have tried everything but still not working :( sorry for my bad english 我已经尝试了一切,但仍然无法使用:(对不起,我的英语不好

-- Jquery code: -jQuery代码:

var counter = 0;
$('#group-input').keypress(function(e) {
  if(e.which == 13) {
    if($('#group-input').val().length > 3) {
    $( "div#box-ins-group" ).append('<div class="element" id="'+counter+'">'+$("#group-input").val()+'</div>');
    $('#group-input').val(''); 
    counter++; 
   }
  }  
}); 

counter is a variable 计数器是一个变量

You need to set float: left on the input, and then change this in your js: 您需要在输入中设置float:,然后在js中进行更改:

$( "div#box-ins-group" ).append

to: 至:

$( "input#group-input" ).before

The problem is that you're appending those elements to the element that contains both the .element divs and the input, so even if you'd fixed the float issue on the input, the new .element divs would always appear after the input in the DOM. 问题是,您要将这些元素追加到同时包含.element div和输入的元素上,因此,即使您将float问题固定在输入上,新的.element div也会始终出现在输入中DOM。 Here's a fiddle . 这是一个小提琴

It's also worth noting that you can remove display: inline-block, as it's ignored when you use floats. 还值得注意的是,您可以删除display:inline-block,因为在使用浮点数时会忽略它。

Seems that the pills are floating but the input is not. 似乎药是漂浮的,但输入不是。 Try this (if you haven't already): 试试这个(如果您还没有的话):

div.container div.edit-local div.form table tr td>div.box>input#group-input {
    height: 11px;
    /*border: none;*/
    font-size: 12px;
    outline: none;
    vertical-align: top;
    width:8px; 
    float: left;
}

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

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