1)我有一个textarea
地方,我正在写一个函数来执行自动调整大小....(效果很好)
2)单击按钮后,我有一个按钮可以改变class
文本区域的颜色(添加背景色,填充等)
问题
点击该按钮后,一切都运行良好,但padding-bottom
appers是0
当supossed显示新40px
填充底部。仅在在textarea上写了点东西之后,才恢复此填充。
我知道这是因为autosize function
我添加到了textarea
作为解决方案,我想.trigger("keypress");
在单击按钮后触发一个,但是什么也没有
我究竟做错了什么?
$('textarea').on('input', autosize);
function autosize() {
var $this = $(this);
$this
.css({
height: 'auto'
})
.css({
height: $this.prop('scrollHeight')
});
}
$(".BgChanger").on("click", function() {
$(".main_textarea")
.addClass("BgA");
});
.BgChanger {}
.BgA {
width: auto;
border: 1px solid red;
background: green!important;
text-align: center!important;
font-size: 22px!important;
color: white!important;
font-weight: bold;
padding: 50px 20px 40px 20px;
}
.main_textarea::-webkit-scrollbar {
display: none;
}
.main_textarea {
width: 300px;
height: 90px;
outline: 0;
border: 1px solid blue;
margin: 0 auto;
resize: none!important;
background: #fff;
outline: 0;
color: #292F33 !important;
font-size: 20px !important;
box-sizing: border-box;
overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="BgChanger">Change Textarea Bg</div>
<textarea class="main_textarea"></textarea>
问题不是填充,而是textarea的height属性。
一种骇人听闻的解决方案是将其链接.css({height: 'auto'})
到您的onclick处理函数。
$('textarea').on('input', autosize);
function autosize() {
var $this = $(this);
$this
.css({
height: 'auto'
})
.css({
height: $this.prop('scrollHeight')
});
}
$(".BgChanger").on("click", function() {
$(".main_textarea")
.addClass("BgA")
.css({
height: 'auto'
});
});
.BgChanger {}
.BgA {
width: auto;
border: 1px solid red;
background: green!important;
text-align: center!important;
font-size: 22px!important;
color: white!important;
font-weight: bold;
padding: 50px 20px 40px 20px;
}
.main_textarea::-webkit-scrollbar {
display: none;
}
.main_textarea {
width: 300px;
height: 90px;
outline: 0;
border: 1px solid blue;
margin: 0 auto;
resize: none!important;
background: #fff;
outline: 0;
color: #292F33 !important;
font-size: 20px !important;
box-sizing: border-box;
overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="BgChanger">Change Textarea Bg</div>
<textarea class="main_textarea"></textarea>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句