在 Javascript 中使用长字符串文字的最佳方法是什么?
可能的重复:
Javascript 中的多行字符串
在 Ruby 中你可以这样做:
temp = <<-SQLCODE
select * from users
SQLCODE
这样你就有了很长的字符串代码中的文字,而不必转义大量字符。 JavaScript中有类似的东西吗?
目前我有这样的 JavaScript 代码,它让我发疯......
new Element.update(lightbox_id, " \
<div id='overlay' class='overlay' > \
</div> \
<div id='lightbox' class='lightbox'> \
<div class='lightbox_title'> \
<div class='corner_image' onclick=\"close_lightbox();return false;\"><a href='#'>" + corner_image + "</a></div> \
<div class='lightboxname' id='lightboxname'>" + title + "</div> \
<div class='close_image'> \
<a href='#' onclick=\"close_lightbox();return false;\">Close</a> or Escape key\
</div> \
</div> \
<div id='" + lightbox_content_id + "' class='lightbox_content'> \
</div> \
<script> \
watch_for_escape(); \
</script> \
</div> \
");
Possible Duplicate:
Multiline strings in Javascript
In Ruby you can do something like this:
temp = <<-SQLCODE
select * from users
SQLCODE
This way you have very long string literals in your code without having to escape lots of characters. Is there something similar in JavaScript?
Currently I have JavaScript code like this, and it's driving me nuts...
new Element.update(lightbox_id, " \
<div id='overlay' class='overlay' > \
</div> \
<div id='lightbox' class='lightbox'> \
<div class='lightbox_title'> \
<div class='corner_image' onclick=\"close_lightbox();return false;\"><a href='#'>" + corner_image + "</a></div> \
<div class='lightboxname' id='lightboxname'>" + title + "</div> \
<div class='close_image'> \
<a href='#' onclick=\"close_lightbox();return false;\">Close</a> or Escape key\
</div> \
</div> \
<div id='" + lightbox_content_id + "' class='lightbox_content'> \
</div> \
<script> \
watch_for_escape(); \
</script> \
</div> \
");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所指的语法通常称为 here-document (或 HEREDOC) ,不,它在 JavaScript 中不可用。
像您一直在做的那样添加反斜杠是在 JavaScript 中跨多行字符串的正确方法。
The syntax you are referring to is often known as here-document (or HEREDOC) and no, it is not available in Javascript.
Adding a backslash as you have been doing is the appropriate way to span strings over multiple lines in JavaScript.
像这样内联 html 是不好的做法,但如果您真的想更干净地处理它,请尝试以下操作:
在页面上放置一个隐藏的 div 并使用您想要的 html,并将自定义参数替换为 {title} 之类的内容。 调用 update 时,传递 yourdiv.innerHTML.replace(...
Having html inline like that is bad practice, but if you really want to handle it cleaner, try this:
Place a hidden div on your page with the html you want, and replace the custom params with something like {title}. When calling update, pass yourdiv.innerHTML.replace(...
如果您使用 Rails,那么使用 rjs 用一些多行 HTML 更新 div 会变得更加清晰:
然后
overlay.html.erb
将包含上面的原始 html 而不会转义。额外的好处是片段“覆盖”也可以在初始页面加载中使用,这在很多情况下都是您想要的。
If you're using Rails, then it becomes a lot cleaner with rjs to update a div with some multiline HTML:
then
overlay.html.erb
would contain the raw html above without escaping.The added benefit is that the fragment 'overlay' could also be used in the initial page load as well, which in a lot of cases is what you want.