首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多行追加Jquery

使用多行追加Jquery
EN

Stack Overflow用户
提问于 2009-02-22 07:34:51
回答 5查看 4.1K关注 0票数 0

我一直在做一个项目,它使用ASP.NET动态创建一个javascript文件,该文件可以从另一个站点调用。

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

代码语言:javascript
复制
$(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生成。(如果这有助于让您了解我在做什么)。

有没有办法把这个大段变成一个可以插入到append脚本中的字符串?

谢谢

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-02-22 08:58:30

跨域jquery json

http://docs.jquery.com/Release:jQuery_1.2/Ajax#Cross-Domain_getJSON_.28using_JSONP.29

票数 1
EN

Stack Overflow用户

发布于 2009-02-22 07:52:44

一些想法:

  • 你可以用空格代替新行,然后创建一条很大的行。这应该不会有问题。
  • 使用字符串连接。拆分字符串和行,然后执行以下操作:

line2 html = line1 + var + ...linen;

  • 调用Ajax来填充div:

$("#divname").load(service_url);

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

在我看来,第三个选项比其他选项更好。

票数 1
EN

Stack Overflow用户

发布于 2009-02-22 07:52:13

如果我错了,请纠正我,但我认为,无论字符串有多少行,起始引号和结束引号之间的所有内容都将被视为该字符串的一部分。除非你的字符串本身有任何引号,在这种情况下,最好在你的字符串上做等同于ASP中的php's addslashes()函数,它应该在字符串中的所有"标记之前添加一个\

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/574530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档