【问题标题】:Focus textarea using css使用 css 聚焦文本区域
【发布时间】:2016-07-31 21:34:55
【问题描述】:

我正在搜索一个 javascript 或 html 或 css 代码,当我点击 textarea 时会更改背景颜色我需要类似 http://prntscr.com/bzsqgq

我尝试使用 javascript,但我无法做到这一点,我尝试使用 rgba 的 onclick 更改背景颜色,例如模糊。

$('.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;');
});

谢谢谁能帮助我。

【问题讨论】:

  • 没必要只用css
  • 你评论表达你的意见..好的

标签: javascript html css


【解决方案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>

【讨论】:

    【解决方案2】:

    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"; 
    }
    .cont{
     width:200px;
     background:red;
     padding:100px;
    }
    input{
      margin:10px;
      }
    .cont.active{
     background:black;
    }
    <div class="cont">
      <input class="chg_parent_on_focus"/>
    </div>

    【讨论】:

    • 我不想改变 textarea 背景颜色 我想改变像这样的正文颜色 prntscr.com/bzsqgq
    • 我知道,请稍等
    猜你喜欢
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 2019-05-10
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多