簡單實現分行輸出的javascript程式碼

iDotNetSpace發表於2008-09-25
 在c#裡只要在字串前加上@符號,變可以讓字串隨意換行,如下程式碼所示:

簡單實現分行輸出的javascript程式碼
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtstring stroutput=string.Format(@"
                    
                        
                    
                    
                        
                    
                    
                        
                    
                    
                        
                    
                    
                        
                    
                    
 你好!{0}  [ 個人資訊 , 退出 ]
 積分:{2}  
 等級:{3}

 新短訊息 {4} , 收藏夾 , 管理
"string.IsNullOrEmpty(userInfo.Nickname) ? userInfo.Username : userInfo.Nickname, userInfo.Password.Substring(48).Trim(), userInfo.Credits, UserGroups.GetUserGroupInfo(userInfo.Groupid).Grouptitle, userInfo.Newpmcount, Urls.UserInfoAspxRewrite(userInfo.Uid));

  而javascript裡是不支援字串的換行的,所以要將stroutput這個字串輸出通常需要一行一行來,在這我用了string.Split的方法來實現避免一行一行的手工輸出:

簡單實現分行輸出的javascript程式碼
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt        private void write(HttpContext context, string line)
        {
            line 
= line.Remove(line.Length - 11);
            context.Response.Write(
string.Format("document.write(\"{0}\");\r\n",line));
        }

 

簡單實現分行輸出的javascript程式碼
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt                   string stroutput=string.Format(@"
                    
                        
                    
                    
                        
                    
                    
                        
                    
                    
                        
                    
                    
                        
                    
                    
 你好!{0}  [ 個人資訊 , 退出 ]
 積分:{2}  
 等級:{3}

 新短訊息 {4} , 收藏夾 , 管理
"string.IsNullOrEmpty(userInfo.Nickname) ? userInfo.Username : userInfo.Nickname, userInfo.Password.Substring(48).Trim(), userInfo.Credits, UserGroups.GetUserGroupInfo(userInfo.Groupid).Grouptitle, userInfo.Newpmcount, Urls.UserInfoAspxRewrite(userInfo.Uid));
                    
string[] aroutput =stroutput.Split('\n');
                    
foreach (string temp in aroutput)
                    {
                        write(context, temp);
                    }

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-464308/,如需轉載,請註明出處,否則將追究法律責任。

相關文章