【问题标题】:Limit count of elements in div限制 div 中元素的数量
【发布时间】:2017-03-13 12:13:13
【问题描述】:

我的 div 有问题

这里是

这是它的代码

<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

我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    您可以使用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 将其限制为当前父元素:

    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.");
     }
    

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

    【讨论】:

    • 优雅的...链接到 jquery documentation 会增加答案的美感:)
    • 按您的意愿添加 :)
    • 无效。我这样写&lt;script type="text/javascript" &gt; $('.title').on('click', function () { $(this).next().toggle(); }); if ($(this).closest(".listdivright").find(".title").length &lt; 10) { var elem = $(this).closest('.title'); $('.title2').append($(elem).html()).show(); } else { alert("No more than 10 allowed."); } $('.click2').click(function () { var elem = $(this).closest('.title'); $('.title2').append($(elem).html()).show(); }); &lt;/script&gt;
    • 您需要检查控制台以查看是否出现任何错误,答案应该让您知道该怎么做,不必只是复制粘贴就可以为您工作,您可能需要调整一点点
    • 然后尝试调试点击事件代码看看哪里出了问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2015-01-18
    • 2018-09-29
    • 1970-01-01
    相关资源
    最近更新 更多