gpt4 book ai didi

jquery - 为什么jquery函数无法对具有相同ID的两个图像执行

转载 作者:行者123 更新时间:2023-12-01 06:29:16 24 4
gpt4 key购买 nike

我的 asp.net MVC 应用程序中有以下脚本来弹跳图像

   $(document).ready(function () {
$("#to-get-bigger").mouseover(function () {
$(this).effect("bounce", { time: 3, distance: 40 });
});
});

在 View 上我有以下两个具有相同 ID 的图像:-

<a href="@Url.Action("StartAssessment", "StartAssessment", new { assessmentid = Model.AssessmentID })" 
onclick = "return confirm('This will make the Assessment avilable for the regestred user.')" >
<img id = "to-get-bigger" border="0" src="@Url.Content("~/content/images/assessment-button1.jpg")" alt="start assessment" />
</a>

<a href="@Url.Action("StartAssessment", "StartAssessment", new { assessmentid = Model.AssessmentID })"
onclick = "return confirm('This will make the Assessment avilable for the regestred user.')" >
<img id = "to-get-bigger" border="0" src="@Url.Content("~/content/images/assessment-button1.jpg")" alt="start assessment" />
</a>

但问题是,Jquery 函数只会执行并反弹一个图像,而它不适用于第二个图像,尽管我已经读到,在这种情况下,Jquery 选择器是 #to-get-bigger 将返回具有此 ID 的所有元素,并且一旦用户将鼠标移动到任何预期元素上,Jquery 函数就会被执行?BR

最佳答案

多个元素具有相同的 id 是无效的,并会导致此类问题。 id 属性应该是唯一的。要使用单个属性引用多个元素,请使用:

HTML

<a href="@Url.Action("StartAssessment", "StartAssessment", new { assessmentid = Model.AssessmentID })" onclick="return confirm('This will make the Assessment avilable for the regestred user.')" > 
<img class="to-get-bigger" border="0" src="@Url.Content("~/content/images/assessment-button1.jpg")" alt="start assessment" />
</a>

<a href="@Url.Action("StartAssessment", "StartAssessment", new { assessmentid = Model.AssessmentID })" onclick="return confirm('This will make the Assessment avilable for the regestred user.')">
<img class="to-get-bigger" border="0" src="@Url.Content("~/content/images/assessment-button1.jpg")" alt="start assessment" />
</a>

jQuery

$(document).ready(function () {
$(".to-get-bigger").mouseover(function () {
$(this).effect("bounce", { time: 3, distance: 40 });
});
});

关于jquery - 为什么jquery函数无法对具有相同ID的两个图像执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9669737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com