$(function () {
$('textarea').setAttribute('style', 'height:' + ($('textarea').scrollHeight) + 'px;');
});

<textarea>
This is very, very long preloading text, I mean vvveeerrryyy, vvveeerrryyy long one!
</textarea>
&#13;
我做错了什么,拜托?我仔细检查了一下,但找不到任何错误
答案 0 :(得分:2)
attr()
。$('textarea').scrollHeight
更改为$('textarea')[0].scrollHeight
。因为它返回undefined setAttribute()
未在jquery中定义函数。而且不要忘记在你的HTML中添加jquery库。
$(function () {
$('textarea').attr('style', 'height:' + ($('textarea')[0].scrollHeight) + 'px;');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>This is very, very long preloading text, I mean vvveeerrryyy, vvveeerrryyy long one!</textarea>
答案 1 :(得分:0)
jQuery没有函数setAttribute()
。使用attr()
$(function() {
$('textarea').attr('style', 'height:' + ($('textarea').scrollHeight) + 'px;');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>This is very, very long preloading text, I mean vvveeerrryyy, vvveeerrryyy long one!</textarea>
&#13;
答案 2 :(得分:0)
尝试以下jquery
/**
* Many drivers have Many langauges (OWNING SIDE)
*
* @ORM\ManyToMany(targetEntity="Language", inversedBy="language", cascade={"persist"})
* @ORM\JoinTable(name="bus_drivers_languages",
* joinColumns={@ORM\JoinColumn(name="bus_driver_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="languages_id", referencedColumnName="id")}
* )
**/
private $languages;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="BusDriver", mappedBy="languages", cascade={"persist"})
*/
protected $language;
这是工作的jsfiddle:https://jsfiddle.net/1vpxmg4m/
答案 3 :(得分:0)
您可以使用jQuery height()
$('textarea').height(function(){
return this.scrollHeight;
});