简体   繁体   English

如何作为参数从HTML大块HTML代码传递给JS函数?

[英]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.

相关问题 如何将运行时参数从html代码传递到javascript函数 - how to pass a runtime parameter from html code to javascript function 如何在 javascript function 中将 html 代码作为参数传递 - How pass html code as parameter in javascript function 如何将 ExpressJS 局部变量传递给 EJS 模板并将其用作从 onload HTML 事件调用 JS function 的参数 - How to pass ExpressJS locals variable to an EJS template and use it as parameter for calling a JS function from onload HTML event 如何使用JavaScript输出大型HTML块? - How to output large HTML chunk using JavaScript? 如何将字符串值从html作为函数参数传递给javascript? - how to pass string value to javascript as function parameter from html? 如何从html调用javascript函数并通过它传递参数 - how to call javascript function from html and pass parameter through it 如何将 function 作为 HTML 中的参数传递? - How do I pass a function as parameter in HTML? 如何将值从js传递到html.erb渲染函数 - How to pass value from js to html.erb render function 如何将变量从我的控制器传递到 HTML,然后传递到 JS 函数? - How to pass a variable from my controller, to the HTML, then to a JS function? 无法将HTML集合作为Js函数中的参数传递? - Can't pass HTML Collection as a parameter in Js function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM