日期:2014-05-16  浏览次数:20677 次

如何判断span里面的内容是否换行?
问题如下:
<span   id   =   "span1 "> te
xt </span>
这种情况下,如何判断出文本已经换行?

------解决方案--------------------
<html>
<head>
<title> t </title>
</head>
<body>
<span id = "span1 " style= "width:30 ">
高级网页制作初级编程(1)
</span>
<input type= "button " value= "test1 " onclick= "alert(span1.innerText.split( '\n ').length) " />
<input type= "button " value= "test2 " onclick= "alert(span1.offsetHeight / 18) " />
</body>
</html>

span1.innerText.split( '\n ').length==1
span1.offsetHeight / 18 ==11

span1.innerText.split( '\n ').length是文本物理的行数
span1.offsetHeight / 18 是实际显示的行数(因为css不同, 18这个数据可能需要调整)

当span1.offsetHeight / 18 > span1.innerText.split( '\n ').length时说明文显示时出现了换行