html文本溢出自动换行,解决一行文本溢出隐藏点击展开之后全部显示并自动换行每行长度一致问题...

一行数据文本内容太多,把页面撑得很长影响美观。该方法可以实现当一行文本内容超过固定长度后,收缩起来,显示一个“展开”按钮,用户一点击后就显示全部内容。当然多行文本也同样适用,(若是全部是中文也可以使用判断判断字符串长度的方法,中文占用两个字符,但是若文章中含有英文就不适合了,因为字母i所占用的长度非常短一个汉字所占用的字符多余2个i,就会出现长短不一的情况;)本文所展示方法解决了这个问题。

HTML

first my gaze toward the moon, but the moon shines on the ditch. “我本一心向明月,奈何明月照沟渠”

展开

收起

css

#content{

width: 150px;

height: 25px;

float:left;

overflow: auto;

word-wrap:break-word;

word-break: break-all;

text-overflow:ellipsis;

white-space:nowrap;

}

a{

float:right;

margin-left: 30px;

position: absolute;

top: 0;

right: 0;

}

#pack{

display: none;

}

JS

window.οnlοad=function(){

// element.scrollHeight---文章内容的实际高度 element.clientHeight---文章内容的显示高度

// element.scrollWidth---文章内容的实际宽度 element.clientWidth ---文章内容的显示宽度

var element=document.getElementById("content")

if(element.scrollHeight>element.clientHeight){

element.style='overflow:hidden;'

}else{

document.getElementById("expand").style="display:none"

}

}

function more(){

document.getElementById("content").style='overflow:visible; white-space:normal;'

document.getElementById("expand").style="display:none"

document.getElementById("pack").style="display:block"

}

function pack(){

document.getElementById("content").style='display:block;overflow:hidden'

document.getElementById("expand").style="display:block"

document.getElementById("pack").style="display:none"

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值