对于此dom元素,Jquery toggleClass运行多次而不是一次(Jquery toggleClass runs multiple times instead of once for this

系统教程 行业动态 更新时间:2024-06-14 16:57:40
对于此dom元素,Jquery toggleClass运行多次而不是一次(Jquery toggleClass runs multiple times instead of once for this dom element)

我有一个输出一堆div的mvc视图。 都有一类userproject。 这是div上唯一的课程。 然而,当我调用toggleClass时,它应该将div从一个小div设置为一个大div,以便查看所有文本。 动画在同一div上的每个div上运行一次。

jQuery的

$(document).ready(function () { $(".userproject").click(function (e) { $(this).toggleClass("userprojectclicked", "slow"); }); });

CSS

.userproject { float: left; border:2px solid; border-radius:5px; width: 270px; padding: 15px; margin: 5px; position: relative; background-color: green; overflow:hidden; } .userprojectclicked { width: 900px; background-color: white; border-color: green; }

在循环中查看以下div输出。

<div class="userproject"> <p>@Html.LabelFor(m => @Model.Name) : @Html.DisplayFor(m => Model.Name)</p> <p>@Html.LabelFor(m => @Model.Description) : @Html.DisplayFor(m => Model.Description)</p> <p>@Html.LabelFor(m => @Model.UserId) : @Html.DisplayFor(m => Model.UserId)</p> <p>@Html.LabelFor(m => @Model.CategoryId) : @Html.DisplayFor(m => Model.CategoryId)</p> </div>

问题是,动画在页面上的每个div都会发生一次......但是它发生在同一个div中,所以它变得大而小......大而小......等等有谁知道为什么?

I have an mvc view that outputs a bunch of divs. All have a class of userproject. That is the only class on the divs. However when I call toggleClass which is supposed to animate the div from a small div to a big div in order to see all the text. The animation runs once for each div on the page on the same div.

jquery

$(document).ready(function () { $(".userproject").click(function (e) { $(this).toggleClass("userprojectclicked", "slow"); }); });

css

.userproject { float: left; border:2px solid; border-radius:5px; width: 270px; padding: 15px; margin: 5px; position: relative; background-color: green; overflow:hidden; } .userprojectclicked { width: 900px; background-color: white; border-color: green; }

view with the below div output in a loop.

<div class="userproject"> <p>@Html.LabelFor(m => @Model.Name) : @Html.DisplayFor(m => Model.Name)</p> <p>@Html.LabelFor(m => @Model.Description) : @Html.DisplayFor(m => Model.Description)</p> <p>@Html.LabelFor(m => @Model.UserId) : @Html.DisplayFor(m => Model.UserId)</p> <p>@Html.LabelFor(m => @Model.CategoryId) : @Html.DisplayFor(m => Model.CategoryId)</p> </div>

The problem is that the animation occurs once for every div on the page ... but it happens to the same div so it grows big and then small ... big and then small ... etc. Does anyone know why?

最满意答案

jquery处于局部视图中,在循环内的常规视图中呈现。 这意味着在原始视图中放置了许多jquery代码副本。 所以jquery的每个副本只运行一次,但由于页面的结构,我提供了很多函数。 将来我可能不会在我的部分视图中包含jquery / javascript。

The jquery was in a partial view that was rendered in a regular view inside a loop. This meant that there was many copies of the jquery code placed in the original view. So each copy of the jquery was only running once but I had many functions rendered because of how the page was structured. In the future I will likely not be including jquery / javascript in my partial views.

更多推荐

本文发布于:2023-04-13 12:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/785c4cf94481d9b60df584f9457fa317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:于此   而不是   元素   Jquery   toggleClass

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!