jquery - 尝试在动态表单上设置焦点输入标签的样式

标签 jquery css forms label

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/

相关文章:

jquery - 复选框在 10 个结果后不会触发带数据表插件的 jquery 函数

javascript - ajax 发布到 php 并检索 sql 结果

html - 如何使驯鹿居中

html - 如何在我的 HTML 页面中包含 ZIP 文件夹中的 CSS 或 JS 文件?

html - ASP :gridview header vertical alignment using css

asp.net-mvc - ASP.NET MVC - 使用相同的表单来创建和编辑

jquery - 带有 Bootstrap 类的固定标题 html 表

jquery - 防止表单基于宽度提交

javascript - 使用 jQuery 更改选择选项时更改占位符

javascript - 使用 JSONP 请求无效的 JSON 文件