FireFox文本自动换行处理,如何实现FireFox文本自动换行
文本自动换行IE中解决方法:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
word-wrap:break-word;
word-break:break-all;
注:在要换行的内容相应的单元格或者DIV里加入,如:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<div style=word-wrap:break-word;></div>或<td style=word-wrap:break-word;></td>
文本自动换行FireFox中解决方法(脚本):
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<script type="text/javascript">
function toBreakWord(intLen, id){
var obj=document.getElementById(id);
var strContent=obj.innerHTML;
var strTemp="";
while(strContent.length>intLen){
strTemp+=strContent.substr(0,intLen)+"<br>";
strContent=strContent.substr(intLen,strContent.length);
}
strTemp+= strContent;
obj.innerHTML=strTemp;
}
</script>
注:以上脚本放在</head>前面.
调用时如下写法:
同个页面单处调用:
<div id="content">这里是要应用换行的内容</div><script language="javascript">toBreakWord(60, "content");</script>
同个页面多处调用:
<div id="content">这里是要应用换行的内容</div><script language="javascript">toBreakWord(60, "content");</script>
<div id="content2">这里是要应用换行的内容</div><script language="javascript">toBreakWord(60, "content2");</script>
注:把应用的JS写在</div>后面,其中60表示一行要显示多少字字符,注意多个调用时ID的相应变化,不能同一个ID名称,应用上面的方法后IE也会是按设定的字符数换行,但是IE里面支持自动换行,所以只要判断一下是否为IE,如果不是IE就不要输出上面的
<script language="javascript">toBreakWord(60, "content");</script>
这段JS,如果不是就要输出。
我现在的做法是,建一个自定义用户控件
控件页面代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MultiTextControl.ascx.cs" Inherits="Controls_MultiTextControl" %>
<div style="width:<%=Width %>px;word-wrap:break-word; overflow:auto;" class="<%=CssClass %>">
<%=Text%>
</div>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// Author:Jolly-Zhang
/// </summary>
public partial class Controls_MultiTextControl : System.Web.UI.UserControl
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
private string strText;
public string Text
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return GetText(); }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strText = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int iWidth = 100;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Width
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return iWidth; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ iWidth = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int iHeight;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Height
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return iHeight; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ iHeight = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string strCssClass;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string CssClass
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return strCssClass; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ strCssClass = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private bool isReplace;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 是否替换尖括号
/// </summary>
public bool IsReplaceLTGT
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return isReplace; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ isReplace = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断当前浏览器是IE还是火狐,
/// 如果是IE则不做做处理,因为IE支持样式换行
/// 如果是火狐就判断其每行是否大于宽(除以11),大于则自动加换行符。
/// </summary>
/// <returns></returns>
protected string GetText()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string strContent = string.Empty;
if (string.Compare(Request.Browser.Browser, "IE", StringComparison.OrdinalIgnoreCase) == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strContent = strText;
if (isReplace)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strContent = strContent.Replace("<", "<").Replace(">", ">").Replace("\r\n", "<br>").Replace("\n", "<br>");
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] separator =
{ "\r\n","\n" };
string[] strWrap = strText.Split(separator, StringSplitOptions.None);
int iMaxLength = (iWidth / 11);
StringBuilder strTemp = new StringBuilder();
for (int i = 0; i < strWrap.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strContent = strWrap[i];
while (strContent.Length > iMaxLength)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strTemp.Append(strContent.Substring(0, iMaxLength) + "\r\n");
strContent = strContent.Substring(iMaxLength);
}
strTemp.Append(strContent + "\r\n");
}
strContent = strTemp.ToString();
if (isReplace)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strContent = strContent.Replace("<", "<").Replace(">", ">").Replace("\r\n", "<br>");
}
}
return strContent;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)