使用CSS聚焦textarea

时间:2016-07-31 13:08:20

标签: javascript html css

我正在搜索一个javascript或html或css代码,当我点击textarea时会改变背景颜色我需要这样的东西http://prntscr.com/bzsqgq

我尝试使用javascript,但我无法做到这一点,我尝试用rgba改变背景颜色,就像模糊一样。

$('.comment .body').on('click', function() {
    if($(this).hasClass('changing')) {
        $('.comment textarea').blur();
    }else {
        $('.comment textarea').focus();
    }

});


$('.comment textarea').on('focus',function() {
    $('.comment .body').addClass('changing').style('background-color: #FF0000;');
});         

$('.change-stamp textarea').on('blur',function() {
    $('.comment .body').removeClass('changing').text('background-color: transparent;');
});

谢谢谁能帮助我。

2 个答案:

答案 0 :(得分:1)

像这样添加wrapperinner元素

.wrapper {
  display: inline-block;
  position: relative;
  padding: 20px;
}
input:focus ~ .inner,
textarea:focus ~ .inner {
  position: absolute;
  z-index: -1;
  left: 0; top: 0;
  width: 100%; height: 100%;
  background: yellow;
  border: 2px solid red;
  box-sizing: border-box;
}
<div class="wrapper">
  <input type="text">
  <br/>
  <br/>
  <textarea></textarea>
  <div class="inner"></div>
</div>

答案 1 :(得分:0)

&#13;
&#13;
var element = document.getElementsByClassName("chg_parent_on_focus")[0]
var parent = element.parentElement
element.onfocus = function(){
  parent.className = "cont active";
}
element.onblur = function(){
  parent.className = "cont"; 
}
&#13;
.cont{
 width:200px;
 background:red;
 padding:100px;
}
input{
  margin:10px;
  }
.cont.active{
 background:black;
}
&#13;
<div class="cont">
  <input class="chg_parent_on_focus"/>
</div>
&#13;
&#13;
&#13;