JavaScript HERE-doc 或其他大引用机制?
有没有一种方便的方法来引用 JavaScript 中同时包含单引号和双引号的一大块 HTML?
是否有类似 HERE-doc <
"""
或自定义分隔符 q{}
?
对于这个问题有什么创造性或创造性的解决方案吗
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
有些人不喜欢这样,所以要做好被蔑视和嘲笑的准备,但一个技巧是将你的“大块东西”转储到
块中:
John Resig 在他的模板机制示例中使用了这种技术(或者是令人厌恶的技术,如果你愿意的话)。
您可以根据需要使用“innerText”或“innerHTML”获取内容,或者通过您喜欢的框架的服务获取内容。
编辑 - 请注意,通过 jQuery(与我在下面的评论中所说的相反)
.text()
确实不起作用,尽管我认为它可以应该。请改用.html()
。Some people don't like this, so be prepared for scorn and derision, but one trick is to dump your "big block of stuff" into a
<script language="text">
block:John Resig has used that technique (or that abomination, if you prefer) for examples of his templating mechanism.
You can get at the contents with "innerText" or "innerHTML" as appropriate, or through the services of your favorite framework.
edit — note that via jQuery (contrary to what I said in a comment below)
.text()
does not work, though I think it should. Use.html()
instead.原生不支持。
但既然我们正在讨论让它发挥作用的方法,那么(根据我的经验)可以这样做:
那些反斜杠就可以了。只需确保反斜杠转义字符串中的所有双引号,因为整个块都用它们引用。
请注意,这不会保留换行符,您必须在每行的尾部斜杠之前手动将它们插入为“\n”。另一方面,每行开头的任何空白缩进都将包含在输出中。
事实上,当您必须在脚本(例如 XML)中声明一个长的多行字符串时,这种方法效果最好,而当您需要保持该字符串的格式与您定义的方式完全相同时,这种方法就不太好了。
干杯
Not supported natively.
But since we're talking ways to make it work, here's one that (in my experience) does:
Those back-slashes'll do the trick. Just make sure to backslash-escape any double quotes in your string since the whole block is quoted with them.
Note that this doesn't retain newlines, you have to insert them manually as "\n" prior to trailing slash on each line. On the other hand, any whitespace indentation at the beginning of each line will be included in the output.
Really this works best when you have to declare a long multiline string in a script (e.g. XML), not as good when you need to keep that string formatted exactly the way you define it.
Cheers
JavaScript 无法做到这一点,但 CoffeeScript 是 JavaScript 之上的一个薄层,能。
点击链接并向下滚动到“多行字符串和此处文档”。
JavaScript can't do it but CoffeeScript, which is a thin layer on top of JavaScript, can.
Follow the link and scroll down to "Multiline Strings and Heredocs".
我记得不久前看到一个聪明的解决方案,在函数中使用多行注释:
注意:最后一个撇号是故意不正确的 ;-P
技巧是调用函数的 toString() 方法并解析使用正则表达式输出多行注释。很聪明,但很像波蒂的建议,有点令人厌恶。
我实际上并不认为这个问题是在为生产用途寻找一种真正可行的方法——这是我自己过早下结论的错误——我不太确定为什么你不直接转义相关的字符串文字分隔符。正如 Tim Down 在下面的评论中指出的那样,ECMAScript 第三版将函数的 toString() 定义为依赖于实现的函数。
为了好玩,我决定检查一下浏览器兼容性,这种方法在 IE、Opera、Safari 和 Chrome 中可行,但在 Firefox 中不可行,因为火狐在返回的字符串中不包含注释。 http://jsfiddle.net/2yvXG/
I remember seeing a clever solution a while ago that used multi-line comments in a function:
Note: that last apostrophe is intentionally incorrect ;-P
The trick is to call the function's
toString()
method and parse out the multi-line comment using a regular expression. Clever, but much like Pointy's suggestion, a bit of an abomination.I didn't actually think the question to be looking for a seriously viable method for production uses -- my own fault for jumping to conclusions -- I'm not really sure why you wouldn't just escape the relevant string literal delimiters. As Tim Down pointed out in the comments below, ECMAScript 3rd edition defines toString() for functions as being implementation dependant.
For funsies, I decided to check out browser compatibility and this method is feasible in IE, Opera, Safari and Chrome but not Firefox, which does not include comments in the returned string. http://jsfiddle.net/2yvXG/
ECMAscript 6,现在的标准,允许使用反引号(重音符号)
引用多行文字字符串。不幸的是,这不是
IE 11 支持,因此不应在大多数网站上使用。
(图片来源:Adam Katz,上)
示例:
ECMAscript 6, now the standard, allows use of back-ticks (accent grave)
to quote multi-line literal strings. Unfortunately, this is not
supported in IE 11, so it should not be used on most websites.
(Credit: Adam Katz, above)
Example:
HereDoc For JavaScript
FuncToHereDoc(“delemiter”,带有注释的 HEREDOC 的未调用函数)
用法:
HereDoc For JavaScript
FuncToHereDoc ("delemiter", uncalled function with commented HEREDOC)
Usage:
我实际上设计了一个类似于 user742675 的笨拙变体,您将文本放在 div 中,然后将其可见性设置为无,然后提取内容。但仅仅引用大量 HTML 是不够的,所以我添加了一个功能来获取您在脚本中声明的所有变量,因此如果您有
var a = 'Steve'
,则任何此处文档文本中 $a 的实例将呈现为“Steve”。I actually worked out a kludgy variant similar to user742675, where you put the text in a div, then set its visibility to none, and pull the contents. But just quoting a lot of HTML wasn't enough, so I added a functionality that picked up all the variables you'd declared in your script, so if you had
var a = 'Steve'
, any instance of $a in your heredoc text would be rendered as 'Steve'.根据之前的答案和不同的用例,这里有一个小例子:
https://gist.github.com/拉瓦耶尔/5880516
Based on previous answers and different use cases, here is a small example:
https://gist.github.com/lavoiesl/5880516
我对这个问题感兴趣,因为我想使用 javascript 将新行添加到编辑屏幕(例如,对于多个电话号码)。 (我可以为此使用 ajax,但想避免额外的服务器请求。)
我喜欢 Pointy 关于使用标签来包含要使用的 html 块的答案:
但是当我尝试这样做时,Firefox 和 Chrome 抱怨语法错误。我的解决方案是将“script”标签更改为“div”,通过 CSS 对用户隐藏其显示,并将其移动到正文中。例如:
I was interested in this question because I want to use javascript to add a new row to an edit screen (e.g., for multiple phone numbers). (i could use ajax for this but wanted to avoid an extra server request.)
I like Pointy's answer about using the tag to enclose blocks of html you want to use:
But Firefox and Chrome complained about syntax errors when I tried this. My solution was to change that 'script' tag to a 'div', hide its display from users via css, and move it within the body. e.g.:
简单
缓存示例: - 制作一个简单的模板函数,并保存该函数:
文本(未评估):
example
with cache: - make a simple template function, and save the function:
simple text (not eval'd):
这是你的答案。在页面正文中创建一个 span 或 div,无论是哪个,都可以使用唯一的 id,并将所需的所有文本以及所需的所有引号放入其中。将 span 或 div 的样式设置为“display:none;visibility:hidden;”。然后,当您需要时,它会从 id 获取 DOM 对象,并检索innerHTML 来执行您想要的操作。
Here is your answer. In the body of your page make a span or div, it does not matter which, with a unique id and put all the text you want in it with all the quotes you want. Make the style of the span or div "display:none; visibility:hidden;". Then when you want it get the DOM object from the id and retrieve the innerHTML to do with what you will.