把数据库中的描述字段内容显示在页面上,会发现敲入的空格、HTML实体、换行符等元素不能正常显示,HTML实体、换行还好,可以用PHP函数htmlspecialchars ()、nl2br()来解决,但空格显示不出来,把空格替换成“ ”吧,也显示不理想,
突然发现,textarea可以显示用户输入原生的元素。用一段js代码控制其高度即可,相关代码如下:
样式:
1 |
<style type= "text/css" > |
2 |
.textarea{ width : 300px ; border : 0px ; background : none ;} |
3 |
</style> |
模板数据绑定:
1 |
< textarea class = 'textarea' > |
2 |
<!--{$row['description']}--> |
3 |
</ textarea > |
js代码:
1 |
<script type= "text/javascript" > |
2 |
/** 让textarea 自适应高度 */ |
3 |
$( ".textarea" ).each( function (){ |
4 |
$( this ).css( "height" ,$( this ).attr( "scrollHeight" )); |
5 |
}); |
6 |
|
转载自:http://www.qianyunlai.com/blog/396.html