限制div中元素的数量

时间:2017-03-13 12:13:13

标签: javascript jquery html css asp.net-mvc

我有div问题

就是这样 enter image description here

这是代码

<div class="listdivright">
        <div style="height: 80%; width: 100%; overflow: auto">

                @foreach (var item in Model)
                {
                   <div class="title" style="margin-top:15px;margin-left:15px; margin-bottom: 10px;">
                       <img class="click"   src="@Url.Content("~/Images/plus_plus.png")" />
                        <span>
                           @Html.TextBoxFor(modelItem => item.question, new {@class="testclass", @readonly = "readonly" })
                         </span>

                       <a class="click2"  style="margin-left:10px;">
                           <img src='@Url.Content("~/Images/arrow.png")' />
                       </a>
                       <a style="margin-left:25px;" href='@Url.Action("Edit", "Questions", new {id = item.QuestionId})'>
                           <img src='@Url.Content("~/Images/Edit.png")' />
                       </a>
                       <a href='@Url.Action("Delete", "Questions", new {id = item.QuestionId})'>
                           <img src='@Url.Content("~/Images/Delete.png")' />
                       </a>
                   </div>
                   <div class="content" style="margin-left:60px; width: 80%; height: 100px; background: white">
                        <div style="width: 100%">
                            <div style="float:left; width: 50%;">
                                <b>@Html.LabelFor(modelItem=>item.TimeForAnswer)</b>
                                <b>:</b>
                                <span>@Html.DisplayFor(modelItem=>item.TimeForAnswer)</span>
                            </div>
                            <div style="float:right;width: 50%;">
                                <b>@Html.LabelFor(modelItem => item.TimeForReady)</b>
                                <b>:</b>
                                <b>@Html.DisplayFor(modelItem => item.TimeForReady)</b>
                            </div>
                        </div>
                       <div style="width: 100%; text-align: center;" >
                           <b>@Html.LabelFor(modelItem => item.Retries)</b>
                           <b>:</b>
                           <b>@Html.DisplayFor(modelItem => item.Retries)</b>
                       </div>
                   </div>
                }

        </div>

我使用JS将块移动到左div

JS脚本代码

<script type="text/javascript" >
$('.title').on('click', function () {
    $(this).next().toggle();
});
$('.click2').click(function () {
    var elem = $(this).closest('.title');
    $('.title2').append($(elem).html()).show();

});

这是左div的代码

<div class="listdivleft">
        <div style="height: 80%; width: 100%; overflow: auto">
           <div class="title2" style="margin-top: 15px; margin-left: 15px; margin-bottom: 15px;padding-top: 10px">
                </div>
           </div>
       </div>

我需要限制左div中的块。从0到10。

所以它可以是0或不超过10

我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以使用length属性检查具有类title的元素是否小于10,在这种情况下,只应添加新的html,如:

 if($(".title").length < 10) {
    var elem = $(this).closest('.title');
    $('.title2').append($(elem).html()).show();
 }
 else {
    alert("No more than 10 allowed.");
 }

或者如果在页面上重复使用其他地方的标题类,则使用 listdivright 类元素并结合closestfind将其限制为当前父元素:< / p>

if($(this).closest(".listdivright").find(".title").length < 10) {
    var elem = $(this).closest('.title');
    $('.title2').append($(elem).html()).show();
 }
 else {
    alert("No more than 10 allowed.");
 }

希望它有所帮助,并让您了解如何做到这一点。