javascript – 作为新换行符的灵活高度
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 作为新换行符的灵活高度,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2100字,纯文字阅读大概需要4分钟。
内容图文
![javascript – 作为新换行符的灵活高度](/upload/InfoBanner/zyjiaocheng/810/ffdfb3bd60a5415b88994d1df1dd2be8.jpg)
我以前没见过这个,但我希望能做到.我正试图在文本内部写入文本区域的自动高度.如此处所见……
http://i.stack.imgur.com/oxRfj.jpg
http://i.stack.imgur.com/XVzYT.jpg
希望这些图像解释我的意思.第二幅图像也显示其理论最大高度.
https://jsfiddle.net/nupk26Ly/
.text-f {
border: none;
border-bottom: #183A4C solid 1px;
color: #183A4C;
font: 1rem/2.4rem Georgia, serif;
margin-top: 40px;
resize: none;
height: 40px;
width: 100%;
}
解决方法:
这是使用jquery的解决方案.希望能帮助到你.
function h(e) {
$(e).css({'height':'40','overflow-y':'hidden'}).height(e.scrollHeight);
}
$('textarea').each(function () {
h(this);
}).on('input', function () {
h(this);
});
.text-f {
border: none;
border-bottom: #183A4C solid 1px;
color: #183A4C;
font: 1rem/2.4rem Georgia, serif;
margin-top: 40px;
height: 40px;
width: 100%;
}
.form-btn {
background: #183A4C;
border: solid 1px #183A4C;
color: #fff;
cursor: pointer;
display: block;
font: 1rem/1.333rem Georgia, serif;
height: 30px;
width: 140px;
margin: 10px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<textarea name="Message" class="text-f" placeholder="Describe your project..."></textarea>
<button class="form-btn" type="button">Submit</button>
纯Javascript解决方案(PS:我在textarea标签中添加了一个ID(textarea))
var textarea = document.getElementById("textarea");
textarea.oninput = function() {
textarea.style.height = "";
textarea.style.height = Math.min(textarea.scrollHeight) + "px";
};
.text-f {
border: none;
border-bottom: #183A4C solid 1px;
color: #183A4C;
font: 1rem/2.4rem Georgia, serif;
width: 100%;
resize: none;
height: 40px;
}
<div id="content">
<div id="main-headline">
<h1 class="main-header">Fill out the form or contact me alternatively.</h1>
</div>
<!----- content ------>
<div class="content-container">
<!----- row 1 ------>
<section class="row">
<!----- form ------>
<form name="contact-f" action="#" method="post">
<div class="col-3 c-right-col">
<textarea id="textarea" name="Message" class="text-f" placeholder="Describe your project..."></textarea>
<button class="link-btn form-btn" type="button">Submit</button>
</div>
</form>
</section>
</div>
</div>
内容总结
以上是互联网集市为您收集整理的javascript – 作为新换行符的灵活高度全部内容,希望文章能够帮你解决javascript – 作为新换行符的灵活高度所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。