JavaScript 函数,传递 HTML 作为参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12500680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript function, pass HTML as parameter
提问by sf89
I have two functions in my script, one that outputs some long HTML string, the other one that takes this string as a parameter and processes it.
我的脚本中有两个函数,一个输出一些长 HTML 字符串,另一个将这个字符串作为参数并处理它。
function myFirstFunction() {
//output some HTML
return myHTML;
}
var myHTML = myFirstFunction();
function mySecondFunction(myHTML) {
//do something with parameter
}
For some reason that I can't figure out, the Chrome JS Console keeps giving me the following error: "Uncaught SyntaxError: Unexpected token <"
出于某种我无法弄清楚的原因,Chrome JS 控制台不断给我以下错误: “Uncaught SyntaxError: Unexpected token <”
I thought maybe that was due to the fact, that the outputed HTML was pretty long since it seems to be working with small chunks of HTML. Any thoughts? Thx!
我想这可能是因为输出的 HTML 很长,因为它似乎正在处理小块 HTML。有什么想法吗?谢谢!
回答by sachleen
Here's problem:
问题来了:
myHTML is a HTML stringlike this:
myHTML 是一个像这样的 HTML字符串:
var myHTML ="<div id="foo"><div id='hello'>hello</div><div id="bar">bar'asdf'asf"sdf"&soidf;</div></div>";
That won't work because you have quotes and stuff inside it that are NOT escaped.
那是行不通的,因为里面有引号和未转义的内容。
If you used innerHTML
to get the HTML of an element on the page, this wouldn't be a problem.
如果您曾经innerHTML
在页面上获取某个元素的 HTML,那么这不会成为问题。
回答by Santiago Elvira Ramirez
myHTML is constructed with some < or > extra so verify the html string
myHTML 是用一些 < 或 > 额外构造的,因此请验证 html 字符串
回答by user964409
There is a way to pass an html text as a parameter to javascript function: Just replace all specifics chars in html tag before you pass it to javascript function. Later in javascript you just revert all specific chars back to normal. The trick is to cause javascript recognizes only those chars it excepted.
有一种方法可以将 html 文本作为参数传递给 javascript 函数:在将其传递给 javascript 函数之前,只需替换 html 标记中的所有特定字符。稍后在 javascript 中,您只需将所有特定字符恢复正常。诀窍是让 javascript 只识别它排除的那些字符。
Example:
例子:
[[C#]]
[[C#]]
string urlString = "http://localhost:8698/DotNetNuke_Community_06.02.01_Install/Default.aspx?TabID=157&categoryId=92&newCategoryId=92";
urlString = urlString.Replace("<", "μ");
urlString = urlString.Replace(">", "?");
urlString = urlString.Replace("&", "");
urlString = urlString.Replace(":", "¥");
urlString = urlString.Replace("=", "?");
urlString = urlString.Replace("/", "?");
urlString = urlString.Replace("?", "?");
urlString = urlString.Replace("'", "?");
function(urlString);
[[Javascript]]
[[Javascript]]
function(urlString){
urlString = urlString.replace(/μ/g, "<");
urlString = urlString.replace(/?/g, ">");
urlString = urlString.replace(//g, "&");
urlString = urlString.replace(/¥/g, ":");
urlString = urlString.replace(/?/g, "=");
urlString = urlString.replace(/?/g, "/");
urlString = urlString.replace(/?/g, "?");
urlString = urlString.replace(/?/g, "'");
}
Regards,
问候,
回答by saml
I'm assuming that you're literally trying to pass html, not in a string literal. If you try something like:
我假设您实际上是在尝试传递 html,而不是字符串文字。如果您尝试以下操作:
myFunction(<html><body>...);
That'll definitely through an error. You need to use string literals:
那肯定会通过一个错误。您需要使用字符串文字:
myFunction("<html><body>...");
If you're using html that has quotes in it, you need to escape them or use single quotes:
如果您使用的 html 中包含引号,则需要对它们进行转义或使用单引号:
"<div id="name">"
is not a valid string. Make it either:
不是有效字符串。要么:
"<div id=\"name\">" or
'<div id="name">'