2012-12-20 49 views
-1
的innerHTML的

請幫我 這是我的C#代碼背後如何設置DIV

SpecialOffersDiv.InnerHtml = "<div class="offer_box_wide"> 
            <a href=""><img src="../images/p1.jpg" width="130" height="98" class="img_left" alt="" border="0" /></a> 
            <div class="offer_info"> <span>For Sale 150 000 $</span> 
            <p class="offer"> &quot;Lorem ipsum dolor sit</p> 
            <div class="more"><a href="#">...more</a></div> 
            </div> 
            </div>"; 

回答

3

你的問題是否由兩件事情引起的。首先,字符串中的雙引號需要被轉義。其次,新線路需要適當處理。有兩種方法可以實現這一點。

首先,您可以使用逐字字符串文字指示符,然後將字符串中的每個雙引號標記加倍。

SpecialOffersDiv.InnerHtml = @"<div class=""offer_box_wide""> 
           <a href=""""><img src=""../images/p1.jpg"" width=""130"" height=""98"" class=""img_left"" alt="""" border=""0"" /></a> 
           <div class=""offer_info""> <span>For Sale 150 000 $</span> 
           <p class=""offer""> &quot;Lorem ipsum dolor sit</p> 
           <div class=""more""><a href=""#"">...more</a></div> 
           </div> 
           </div>"; 

其次,你無法逃脫的雙引號用一個反斜槓並更改換行符爲\ n:

SpecialOffersDiv.InnerHtml = "<div class=\"offer_box_wide\">\n" + 
          "<a href=\"\"><img src=\"../images/p1.jpg\" width=\"130\" height=\"98\" class=\"img_left\" alt=\"\" border=\"0\" /></a>\n" + 
          "<div class=\"offer_info\"><span>For Sale 150 000 $</span>\n<p class=\"offer\"> &quot;Lorem ipsum dolor sit</p>\n" + 
          "<div class=\"more\"><a href=\"#\">...more</a></div>\n" + 
          "</div>\n" + 
          "<`div>"; 

我會說去與第一,它看起來更漂亮。

2

String literals(特別是看到的例子string f & g

SpecialOffersDiv.InnerHtml = @"<div class=""offer_box_wide""> 
         <a href=""""><img src=""../images/p1.jpg"" width=""130"" height=""98"" class=""img_left"" alt="""" border=""0"" /></a> 
         <div class=""offer_info""> <span>For Sale 150 000 $</span> 
         <p class=""offer""> &quot;Lorem ipsum dolor sit</p> 
         <div class=""more""><a href=""#"">...more</a></div> 
         </div> 
         </div>";