【问题标题】:Is there a way where I can highlight the field/div of the focused input?有没有办法可以突出重点输入的字段/div?
【发布时间】:2015-03-24 21:04:55
【问题描述】:
<!doctype html>
<html>

<style>
    input:focus { 
        background: yellow;
    }

    div:focus { 
        background: gray;
    }
</style>

    **This section must be highlighted whenever i run the code**
    <div> 
        SOME TEXT<input type = 'text' autofocus>
    </div>
    <div>
        SOME TEXT <input type = 'text'>
    </div> 

</html>

【问题讨论】:

标签: html css focus highlight


【解决方案1】:

您好,我尝试使用 jQuery,但可能还有其他方法可以达到此结果。

首先我创建了一个类.gray,我在jquery 部分中使用它。如果您使用它,请务必包含&lt;script src="http://code.jquery.com/jquery-1.11.2.min.js"&gt;&lt;/script&gt;

CSS

input:focus { 
    background: yellow;
}

.gray {
    background:gray;
}

jQuery

$('input').blur(function(){
    $('input').parent().removeClass("gray");
})

.focus(function() {     
    $(this).parent().addClass("gray")
});

jsFiddle

如果您愿意,可以将 remove/addClass 替换为 css('background', 'gray');

注意旧版浏览器可能不支持此功能,尤其是旧版 IE(10 以下)

input:focus {
  background: yellow;
}
.gray {
  background: gray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script>
  $(document).ready(function() {
    $('input').blur(function() {
      $('input').parent().removeClass("gray");
    })

    .focus(function() {
      $(this).parent().addClass("gray")
    });

  });
</script>
<div>
  SOME TEXT
  <input type="text" value="" autofocus>
</div>
<div>
  SOME TEXT
  <input type="text" value="">
  </div

【讨论】:

  • 我现在知道该怎么做了。非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2017-11-28
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多