Jquery使用多行追加

时间:2009-02-22 07:34:51

标签: javascript jquery

我一直致力于一个使用ASP.NET动态创建javascript文件的项目,该文件是从另一个站点调用的。

这个jquery javascript文件附加一个div并用一个相当大的HTML段填充它,为了做到这一点,我需要将段转换成一个字符串,如下所示:

$(document).ready(function(){   
var html = "Giving this magazine such a lofty epithet may seem a bit presumptuous, but for a non scientifically trained outsider this magazine offers a fresh and challenging look at the fast paced world of science that doesn't shy away from humor and the use of terms and ideas that may require its readers to go online and define a term. And in some cases it may inspire the reader to pick up a book on science by such greats as Hawking and Greene in order to better grasp some of the concepts dealing with time, space and atoms. This magazine isn't dumbed down. It includes well placed and efficient illustrations to help explain some of the more abstract points. It is not designed in the way popular magazinea are, in so much as they only touch upon a topic in the simplest manner and then move on before the audience is lost. Yet this magazine keeps the attention of the reader by combining explanatory notes that help people with no background knowledge have some grasp of the topic and by using humor and well written articles to clearly make their points. <br />For a magazine with a serious and well researched list of topics having small cartoons the likes of the New Yorker shows how comfortable this magazine is with itself. From the moment I picked up this magazine for the first time I felt like every word I read mattered and was worth my time to read. (Not true of many other magazines) American Scientist may not have the audience of Discover or National Geographic, nor is it as accessible as said titles, but for those with a true interest in science willing to challenge themselves and commit to real learning this magazine may be a perfect fit. At $4.95 it is certainly worth it to pick a copy on the news stand and try it out."

$("#divname").append(html);  
}); 

正如您所看到的,该段将非常大,我无法知道从我的数据库动态生成多大,具体取决于用户在其请求中定义的reviewID。

要插入div的html是一个评论列表,由循环器通过列表使用asp.net MVC生成。 (如果这有助于你了解我在做什么)。

有没有办法将这个大段转换成一个可以插入追加脚本的字符串?

谢谢

5 个答案:

答案 0 :(得分:1)

一些想法:

  • 您可以使用空格替换新行并创建一个巨大的行。应该没有问题。
  • 使用字符串连接。拆分字符串和行并执行:

    var html = line1 +            line2 +            ...            亚麻;

  • 进行Ajax调用以填充div:

    $( “#divname”)负载(SERVICE_URL);

您需要创建一个将返回字符串的服务。

在我看来,第三种选择比其他选择更好。

答案 1 :(得分:1)

答案 2 :(得分:0)

如果我错了,请纠正我,但我认为起始和结束引号之间的所有内容都将被视为该字符串的一部分,无论它有多少行。除非你的字符串本身有任何引号,否则在你的字符串中在ASP中执行相当于php's addslashes()的函数会更好,这应该在\之前添加" {{1}} 1}}字符串中的标记。

另一个想法是使用Json对字符串进行编码/解码。

答案 3 :(得分:0)

我没有看到只生成一个大屁股长单行字符串然后像你正在做的那样追加它有什么问题。期。完成。发烧友不会给你任何收获。

答案 4 :(得分:0)

将其隐藏在页面的其他位置,并在需要时用它填充div?