【问题标题】:how to change label to bold when a select element has focus - JQueryhow to change label to bold when a select element has focus - JQuery
【发布时间】:2016-05-18 22:26:11
【问题描述】:

在标签 last name 上,当焦点位于第二个选择元素上时,标签不会变为粗体。关于如何做到这一点的任何提示,

我有这个代码

<form id="formul">
  <label for="a1">First name:</label><br>
  <input type="text" name="firstname" id="a1" class="_lblbold"><br>
  <label for="a2">Last name:</label><br>
  <input type="text" name="lastname" id="a2" class="_lblbold">
  <select id="a2"><option value="">Choose</option></select>
  <select id="a3"><option value="">Choose</option></select><br>
  <label className="col-md-5 lbl_e2bnames" For="id_input_addit">Additional</label>
  <textarea id="id_input_addit" cols="50" rows="7" placeholder="ABCD"></textarea><br>
</form>

jQuery

$('form :input').bind({
    focus: function () {
        var id = $(this).attr('id').toString();
        var add = $("label[for='" + this.id + "']").addClass('labelfocus');
      $("label[]").addClass('labelfocus');


    },
    blur: function () {
        $('label').removeClass('labelfocus');
      $("label").removeClass('labelfocus');
    }
});

CSS

.labelfocus {
  font-weight:bold;
}

这是 Codepen:http://codepen.io/parlop/pen/oxOoba

【问题讨论】:

    标签: jquery select input focus


    【解决方案1】:

    根据您的逻辑,问题在于第二个元素正在使用另一个 ID

    <select id="a3"><option value="">Choose</option></select><br>
    

    将其更改为“a2”

    从概念上讲,使用 2 个或更多具有相同 ID 的元素是错误的,在这种情况下,您可以使用类而不是使用相同的 ID

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-16
      • 2022-12-01
      • 2022-12-01
      • 2017-07-13
      • 2022-12-02
      • 2022-12-27
      • 2019-09-18
      • 2011-09-22
      相关资源
      最近更新 更多