Solved: Seems my issue was caused by CSS inheritance. For the record, the second snippet in my original question actually worked.
我有带有输入和标签的 html 表单。
当我聚焦输入时,我可以使用 jquery 成功更改标签样式。这是使用下面的代码片段完成的:
$("input, textarea").focus(function(){
$("label[for='" + this.id + "']").addClass("labelfocus");
})
.blur(function()
{
$("label").removeClass("labelfocus");
});
但是,这不适用于动态生成的表单。
我试过使用 jquery 的“$(document).on(...”,但这对我也没有用。请参见下面的代码片段:
$(document).on({
focus: function() {
$("label[for='" + this.id + "']").addClass("labelfocus");
},
blur: function() {
$("label").removeClass("labelfocus");
}
}, "input, textarea");
有什么建议吗?谢谢!
最佳答案
如果您使用 $("input, textarea").on
而不是 $(document).on
我认为它会起作用。
关于jquery - 尝试在动态表单上设置焦点输入标签的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17572942/