Div在添加输入时扩展。

huangapple go评论64阅读模式
英文:

Div expands when input is added

问题

When adding a new input field, the width of the fields block increases each time.
How can I fix it without specifying the width for the fields block?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
setInterval(function() {
  var input = '<input type="text">';
  $("#fields").append(input);
}, 2000);

<!-- language: lang-css -->
body {
  background: grey;
  display: flex;
  align-items: center;
  justify-content: center;
}

#fields {
  background: green;
  padding: 20px;
}

#fields input {
  width: 100%;
}

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="fields">
  <input type="text">
</div>

<!-- end snippet -->
英文:

When adding a new input field, the width of the fields block increases each time.
How can I fix it without specifying the width for the fields block?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

setInterval(function() {
  var input = &#39;&lt;input type=&quot;text&quot;&gt;&#39;;
  $(&quot;#fields&quot;).append(input);

}, 2000);

<!-- language: lang-css -->

body {
  background: grey;
  display: flex;
  align-items: center;
  justify-content: center;
}

#fields {
  background: green;
  padding: 20px;
}

#fields input {
  width: 100%;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;fields&quot;&gt;
  &lt;input type=&quot;text&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 3

如果您不想修改inputwidth,您可以将#fields容器的flex属性设置为flex-direction: column;

#fields {
  display: flex;
  flex-direction: column;
  background: green;
  padding: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="fields">
  <input type="text">
</div>
英文:

If you do not want to modify the width of input you can flex the #fields container and specify flex-direction: column;.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

setInterval(function() {
  var input = &#39;&lt;input type=&quot;text&quot;&gt;&#39;;
  $(&quot;#fields&quot;).append(input);

}, 2000);

<!-- language: lang-css -->

body {
  background: grey;
  display: flex;
  align-items: center;
  justify-content: center;
}

#fields {
  display: flex;
  flex-direction: column;
  background: green;
  padding: 20px;
}

#fields input {
  width: 100%;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;fields&quot;&gt;
  &lt;input type=&quot;text&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

使用display: block替代width: 100%

#fields input {
  display: block;
}
英文:

Use display: block instead width: 100%:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

setInterval(function() {
  var input = &#39;&lt;input type=&quot;text&quot;&gt;&#39;;
  $(&quot;#fields&quot;).append(input);

}, 2000);

<!-- language: lang-css -->

body {
  background: grey;
  display: flex;
  align-items: center;
  justify-content: center;
}

#fields {
  background: green;
  padding: 20px;
}

#fields input {
  display: block;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;fields&quot;&gt;
  &lt;input type=&quot;text&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

&lt;!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false --&gt;

&lt;!-- 语言: lang-js --&gt;

    setInterval(function() {
      var input = '&#39;&lt;input type=&quot;text&quot;&gt;&#39;;
      $(&quot;#fields&quot;).append(&quot;&lt;br&gt;&quot;+input);

    }, 2000);

&lt;!-- 语言: lang-css --&gt;

    body {
      background: 灰色;  
    }

    #fields {
      background: 绿色;
      padding: 20px;  
      text-align:center;
    }



    #container{
    width:60%;
    border:solid 2px 绿色;
    margin:0 auto;
    }

&lt;!-- 语言: lang-html --&gt;

    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;div id=&#39;container&#39;&gt;
    &lt;div id=&quot;fields&quot;&gt;
      &lt;input type=&quot;text&quot;&gt;
    &lt;/div&gt;
    &lt;/div&gt;

&lt;!-- 结束代码片段 --&gt;
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

setInterval(function() {
  var input = &#39;&lt;input type=&quot;text&quot;&gt;&#39;;
  $(&quot;#fields&quot;).append(&quot;&lt;br&gt;&quot;+input);

}, 2000);

<!-- language: lang-css -->

body {
  background: grey;  
}

#fields {
  background: green;
  padding: 20px;  
  text-align:center;
}



#container{
width:60%;
border:solid 2px green;
margin:0 auto;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&#39;container&#39;&gt;
&lt;div id=&quot;fields&quot;&gt;
  &lt;input type=&quot;text&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月10日 21:01:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定