html - 有没有一种方法可以突出显示焦点输入的字段/div?

标签 html css focus highlight

<!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>

最佳答案

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

首先我做了一个类.gray我在 jquery 部分使用它。如果你使用它,一定要包括 <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>

CSS

input:focus { 
    background: yellow;
}

.gray {
    background:gray;
}

jQuery

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

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

jsFiddle

如果需要,可以用 css('background', 'gray'); 替换 remove/addClass

note this may not be supported in older browsers especially older versions of IE (below 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

关于html - 有没有一种方法可以突出显示焦点输入的字段/div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29230451/

相关文章:

javascript - 仅在表单提交时显示 HTML5 验证

javascript - 使用 jQuery 添加日期范围选择器并更新内容

"Read More"的 HTML/CSS 可折叠按钮不起作用

javascript - SVG <g> 元素大小设置为零,尽管它有子元素

css - 如何使导航按钮的高度大于其容器高度?

javascript - 为数组显示添加/删除类 - sonarqube

javascript - 将对象数组传递到另一个页面

c# - 如果已经打开,是否关注 winform?

android - 在不丢失 SeekBar 焦点的情况下更新 ListView 行

android - 点击外部编辑文本时更改编辑文本的焦点