Javascript div中元素的限制计数

Javascript div中元素的限制计数,javascript,jquery,html,css,asp.net-mvc,Javascript,Jquery,Html,Css,Asp.net Mvc,我有很多问题要问 给你 这是它的代码 <div class="listdivright"> <div style="height: 80%; width: 100%; overflow: auto"> @foreach (var item in Model) { <div class="title" style="margin-top:15px;

我有很多问题要问

给你

这是它的代码

<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>

@foreach(模型中的var项目)
{
@Html.TextBoxFor(modeleItem=>item.question,新的{@class=“testclass”,@readonly=“readonly”})
@LabelFor(modeleItem=>item.TimeForAnswer)
:
@DisplayFor(modelItem=>item.TimeForAnswer)
@LabelFor(modeleItem=>item.TimeForReady)
:
@DisplayFor(modelItem=>item.TimeForReady)
@LabelFor(modeleItem=>item.Retries)
:
@DisplayFor(modelItem=>item.Retries)
}
我使用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();

});

$('.title')。在('click',函数(){
$(this.next().toggle();
});
$('.click2')。单击(函数(){
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>

我需要将左分区中的块从0限制到10

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


我如何做到这一点?

您可以使用property检查class
title
的元素是否小于10,在这种情况下,只应添加新的html,类似于:

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

希望它对您有所帮助,并让您了解如何执行此操作。

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

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

希望它能帮到你,让你知道怎么做。

。。。jquery的链接会增加答案的美感:)根据您的意愿添加:)不起作用。我是这样写的:
$('.title')。在('click',function(){$(this.next().toggle();});if($(this).closest(“.listdivright”).find(“.title”).length<10{var elem=$(this).closest('.title');$('.title2').append($(elem.html()).show();}else($(允许的数量不超过10。);}$('.click2')。click(函数(){var elem=$(this).closest('.title');$('.title2')。append($(elem).html()).show();})您需要检查conosle以查看是否出现任何错误,答案应该会告诉您如何做,不必只是复制粘贴它就可以为您工作,您可能需要调整一点位,然后尝试调试单击事件代码以查看发生了什么错误。。。jquery的链接会增加答案的美感:)根据您的意愿添加:)不起作用。我是这样写的:
$('.title')。在('click',function(){$(this.next().toggle();});if($(this).closest(“.listdivright”).find(“.title”).length<10{var elem=$(this).closest('.title');$('.title2').append($(elem.html()).show();}else($(允许的数量不超过10。);}$('.click2')。click(函数(){var elem=$(this).closest('.title');$('.title2')。append($(elem).html()).show();})您需要检查conosle以查看是否出现任何错误,答案应该会告诉您如何做,不必只是复制粘贴它就可以了,您可能需要调整一点位,然后尝试调试单击事件代码以查看出现了什么问题