【发布时间】:2016-12-23 07:31:29
【问题描述】:
这是一个ASP.Net MVC 5 项目。
我有一个简单的jQuery 在鼠标悬停时淡入淡出HTML 元素,如图所示:
@Scripts.Render("~/bundles/jquery")
<script>
$('.entrisButton').hover(function ()
{ $(this).fadeTo(1, 1); },
function ()
{ $(this).fadeTo(1, 0.1); }
);
</script>
用于以下部分MyPartialView.cshtmlView(jQuery 脚本在同一个MyPartialView.cshtml 文件中):
<h2>
@Html.Raw(Model.GetHeaderHtml())
<span class="entrisButton">
<small>
<span title="Add new entry" class="glyphicon glyphicon-plus-sign text-success"></span>
<span title="Change this entry" class="glyphicon glyphicon-edit text-primary"></span>
<span title="Delete this entry" class="glyphicon glyphicon-remove-circle text-danger"></span>
</small>
</span>
</h2>
这是jQuery的效果:
这没关系...但是,上面的部分MyPartialView.cshtml 在另一个cshtml 文件中被多次调用,如下所示:
@if (Model != null) {
<hr/>
if (Model.Any()) {
foreach (var item in Model) { //do something foreach item
@Html.Partial("MyPartialView", item);
}
}
}
导致页面呈现如下:
如您所见,字母“a”有三个不同的结果(每个结果呈现一个 MyPartialView.cshtml),每个结果旁边都有三个字形图标 - 已淡出。
现在,前两个“a”显示鼠标悬停时的预期行为:
好的
但最后一个“a”没有显示预期的行为,虽然鼠标悬停在它上面,但淡入淡出不起作用:
不行
我注意到问题发生了,只要上面的查询结果(在这种情况下是第二个“a”)有有序列表(如上面的 1、2、3),那么下面的MyPartialView.cshtml 不显示期望的行为。正如您在我的示例中注意到的那样,第一个“a”没有有序列表 - 因此淡入和淡出有效。第二个“a”有有序列表 - 淡入和淡出也可以。但是第三个“a”是在带有有序列表的查询结果之后 - 它不起作用。
当查询结果只有两个并且第一个具有有序列表时,行为是一致的,然后第二个中的淡入和淡出不显示。
因此,我怀疑有序列表有问题,但我不知道为什么。这是我的MyPartialView.cshtml:
@using MyProject.Models
@using System.Linq;
@model ResultPerPhrase
@Scripts.Render("~/bundles/jquery")
<script>
$('.entrisButton').hover(function ()
{ $(this).fadeTo(1, 1); },
function ()
{ $(this).fadeTo(1, 0.1); }
);
</script>
<h2>
@Html.Raw(Model.GetHeaderHtml())
<span class="entrisButton">
<small>
<span title="Add new entry" class="glyphicon glyphicon-plus-sign text-success"></span>
<span title="Change this entry" class="glyphicon glyphicon-edit text-primary"></span>
<span title="Delete this entry" class="glyphicon glyphicon-remove-circle text-danger"></span>
</small>
</span>
</h2>
@if (Model.results.Count() > 1) {
<ol>
@foreach (var result in Model.results) {
<li>@Html.Raw(result.ToString())
<span class="entrisButton">
<span title="Add new entry" class="glyphicon glyphicon-plus-sign text-success"></span>
<span title="Change this entry" class="glyphicon glyphicon-edit text-primary"></span>
<span title="Delete this entry" class="glyphicon glyphicon-remove-circle text-danger"></span>
</span>
</li>
}
</ol>
} else {
<ul style="list-style: none;" class="adjusted-par">
@foreach (var result in Model.results) {
<li>@Html.Raw(result.ToString())
<span class="entrisButton">
<small>
<span title="Add new entry" class="glyphicon glyphicon-plus-sign text-success"></span>
<span title="Change this entry" class="glyphicon glyphicon-edit text-primary"></span>
<span title="Delete this entry" class="glyphicon glyphicon-remove-circle text-danger"></span>
</small>
</span>
</li>
}
</ul>
}
这里可能出了什么问题?
【问题讨论】:
-
也许你只需要把你的javascript代码放在
$(document).ready(function(){ yourCodeHere })或者$(function(){ yourCodeHere })里面...两者是等价的。 -
你成功了!而已!非常感谢,朋友! :D 但是为什么呢?
-
我要发布一个答案:)
标签: javascript c# jquery asp.net-mvc asp.net-mvc-5