简体   繁体   English

如何 append div 在 jquery 中按行排列?

[英]How to append div to row wise in jquery?

On click on some button I am looking to append the div using ID and I would like to display added items each time row wise, maximum of 6 items in a row.在单击某个按钮时,我正在寻找 append 使用 ID 的div ,我希望每次按行显示添加的项目,最多连续显示 6 个项目。 The below code is adding to bottom each time, not in horizontal wise.下面的代码每次都添加到底部,而不是水平方向。 How can i add as below screenshot?如何添加如下截图?

在此处输入图像描述

html: html:

<div id="dashboardArea" class="wrapper">
</div>

jquery code: jquery代码:

$(document).ready(function(){
$("#saveProjecttoDash").click(function() {
    $('#overlay').fadeOut(300);
    let projectName = document.getElementById("enterprojectName").value;
    let clientName = document.getElementById('clientNametochoose').value;
    var someDiv = '<div class="row"><div class="pull-bottom" style="height:500px;background:#e3e8e7">'+projectName+'<br>'+clientName+'</div></div>';
    $('#dashboardArea').append(someDiv);
  });
});

// css for the below class: // css 用于以下 class:

.pull-bottom {
width: 22%;
margin: 10px 0px 0px 100px;
vertical-align: bottom;
float: none;
    webkit-columns: 2 200px;
     -moz-columns: 2 200px;
          columns: 2 200px;
  -webkit-column-gap: 3em;
     -moz-column-gap: 3em;
          column-gap: 3em;
}

In your pull-bottom CSS class, the float property should be left in this case.在您的pull-bottom CSS class 中,在这种情况下应left float属性。

In the current state, each row will take the full width avalaible.在当前的 state 中,每行将采用全宽度可用。

Fiddle: https://jsfiddle.net/fh42xatu/小提琴: https://jsfiddle.net/fh42xatu/

May be using grid can get you 6 cells.可能使用网格可以获得 6 个单元格。

 $(document).ready(function() { $("#save").on("click", function() { if ($('#area').children().length == 6) return; $("<div>", { class: "cell", append: `Column - ${$('#area').children().length + 1}`, appendTo: "#area" }) }); });
 * { margin: 0; box-sizing: border-box; } #area { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-gap: 1em; }.cell { background: #fafafa; height: 50px; padding: 1em; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="save">SAVE</button> <div id="area"></div>

You can use flex :您可以使用flex

 const NewClientProject = (pName, cName) => { return $("<div>", { class: "cell", append: `${pName||"Project Name"}<br>${cName||"Client Name"}`, appendTo: "#dashboardArea" }); } jQuery( $ => { $("#toDashboard").on("click", function() { $('#overlay').fadeOut(300); NewClientProject($("#pName").val(), $('#cName').val()); }); }); NewClientProject(); NewClientProject();
 * {margin:0; box-sizing:border-box;} #dashboardArea { display: flex; flex-flow: row wrap; } #dashboardArea.cell { background: #e3e8e7; flex: 0 0 22%; margin: 1% 1.5%; min-height: 140px; padding: 10px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="pName" placeholder="Project Name" type="text"> <input id="cName" placeholder="Client Name" type="text"> <button id="toDashboard">Save to Dashboard</button> <div id="dashboardArea"></div>

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

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