Heim >Web-Frontend >HTML-Tutorial >HTML页面加载完后,根据内容调整<textarea>元素的高度_html/css_WEB-ITnose

HTML页面加载完后,根据内容调整<textarea>元素的高度_html/css_WEB-ITnose

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-24 11:58:271468Durchsuche

假如我的HTML中有一个textarea元素,我想在它加载完成后根据内容调整其高度。怎么做呢?

1. 我定义的textarea元素如下

<textarea class="form-control" type="text" name="value" id="value" placeholder="输入参数值"></textarea>
注意: id = 'value'


这里的关键问题是html加载完后会调用哪个函数,有以下两种方法:

2.1 方法一:通过调用$(document).ready(function () {} )

<script type="text/javascript">    function get_text_rows(text) {        return text.split("\n").length;    }        $(document).ready(function () {        document.getElementById('value').rows = get_text_rows($('#value').val());    });</script>

2.2 方法二:通过调用window.onload
<script type="text/javascript">    function get_text_rows(text) {        return text.split("\n").length;    }        window.onload = function() {        document.getElementById('value').rows = get_text_rows($('#value').val());    }</script>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn