-1

请帮我把我的 C# 代码放在后面

  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>";
4

2 回答 2

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>";

我会说第一个,它看起来好多了。

于 2012-12-20T16:52:08.413 回答
2

字符串文字(特别是参见示例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>";
于 2012-12-20T16:48:54.847 回答