JavaScript HERE-doc还是其他大型引用机制? [英] JavaScript HERE-doc or other large-quoting mechanism?
问题描述
有没有一种方便的方法来引用JavaScript中包含单引号和双引号的大块HTML?
Is there a convenient way to quote a large block of HTML that has both single and double quotes in JavaScript?
有什么类似HERE-doc << EOF
,多引号字符,或自定义分隔符
q {}
?
Is there anything like a HERE-doc <<EOF
, a multi-quote character """
, or custom delimiters q{}
?
任意这个问题的创造性或创造性解决方案?
Any creative or inventive solutions to this problem?
推荐答案
有些人不喜欢这样,所以要做好蔑视和嘲笑的准备,但是一招是将你的大块东西转储到< script language =text>
块:
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:
<script id='blockOfStuff' language="text">
Hi this is random stuff
<h1>Including HTML markup</h1>
And quotes too, or as one man said, "These are quotes, but
'these' are quotes too."
</script>
John Resig使用了这种技术(或者说,如果你愿意,可以使用这种技巧)作为模板机制的例子。
John Resig has used that technique (or that abomination, if you prefer) for examples of his templating mechanism.
您可以根据需要使用innerText或innerHTML来获取内容,或者通过t他为您最喜爱的框架提供服务。
You can get at the contents with "innerText" or "innerHTML" as appropriate, or through the services of your favorite framework.
编辑—请注意,通过jQuery(与我在下面的评论中所说的相反) .text()
不工作,但我认为应该。改为使用 .html()
。
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.
这篇关于JavaScript HERE-doc还是其他大型引用机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!