当文本框具有焦点时,我试图更改标签的颜色
<div class="editor-label">
<%: Html.LabelFor(model => model.FirstName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FirstName) %>
<%: Html.ValidationMessageFor(model => model.FirstName) %>
</div>
这是我的 JavaScript
$(':text').focus(function () {
$(this).prevAll("label").last.css("color", "blue");
});
$(this).prevAll()
始终为空。但是 $(this).next()
和 $(this).nextAll()
有一个跨度
请帮忙,我非常感谢任何帮助
最佳答案
$(this).prevAll("label")
为空,因为 prevAll("label")
将查找文本框的同级标签。你应该这样做:
$(':text').focus(function () {
$(this).parent("div").prev().find("label").css("color", "blue");
});
next()
和 nextAll()
返回一个跨度,因为以下代码将呈现为一个跨度,它是文本框之后的下一个元素。
<%: Html.ValidationMessageFor(model => model.FirstName) %>
关于javascript - 上一个在 div 内不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5692280/