[英]how to pass as parameter to JS function from HTML large chunk of HTML code?
I want to pass as argument a large (maybe 2-3 paragraphs of html formatted code) chunk of HTML code to a Javascript function call from HTML. 我想将一大段(可能是2-3段html格式的代码)作为参数传递给HTML的Javascript函数调用。 The problem is, the formatted HTML keeps appearing in the page itself, which shouldnt be the case ! 问题是,格式化的HTML始终出现在页面本身中,事实并非如此! I am assuming theres some problem with single/double quotes ! 我假设单引号/双引号存在一些问题!
And, I am working on Facebook tab page. 而且,我正在Facebook标签页上工作。
Can anyone please help me ? 谁能帮帮我吗 ?
Thanks. 谢谢。
- ahsan -阿三
One way is to have a hidden div (something with display:none), and populate that with your 2-3 paragraphs of html formatted code. 一种方法是拥有一个隐藏的div(带有display:none的东西),并用2-3段html格式的代码填充它。 Then, you can just pass the innerHTML of the div into your function. 然后,您可以将div的innerHTML传递到函数中。 Quotes (of any kind) won't cause a problem in this method. 引号(任何形式)都不会导致此方法出现问题。
Some libraries like icanhaz.js also do something like this: 像icanhaz.js之类的某些库也执行以下操作:
<script type="text/html" id="someHTMLTemplate">
<div>You can put whatever html you want here</div>
<p>And the browser just ignores it</p>
</script>
I use the same technique with mustache.js and then grab the template from the innerHTML of the script tag after grabbing it by the dom id. 我对mustache.js使用了相同的技术,然后在通过dom id抓取模板后从script标记的innerHTML抓取模板。 This has the advantage that the browser doesn't have to parse your extra html while loading it is just parsed when you need to display it in another node on the page. 这样做的好处是,当您需要在页面的另一个节点上显示浏览器时,浏览器不必解析额外的html,只需对其进行解析即可。
Another way is to encode the HTML and then decode it in the JS. 另一种方法是对HTML进行编码,然后在JS中对其进行解码。 Here's an example using the JS escape info: 这是使用JS转义信息的示例:
console.log(escape("<hello></hello>")); // %3Chello%3E%3C/hello%3E
console.log(unescape("%3Chello%3E%3C/hello%3E")); // <hello></hello>
Mind you, if you have an issue with your string quotations to begin with, then there will still be a problem encoding. 提醒您,如果您在一开始的字符串引号有问题,那么编码仍然会出现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.