【发布时间】:2019-07-28 16:04:58
【问题描述】:
(最后的问题) 我正在对 ebay 进行 api 调用并获取 JSON 文件。此 JSON 的值字段之一是以纯文本编写的 html 代码。我需要得到它并将其嵌入 iframe 中。现在,整个代码是根据屏幕上的按钮点击在 javascript 中动态生成的。我的问题是我写的代码没有完全显示执行的html原始文本代码。
JSON 字符串示例
"<font rwr=\"1\" size=\"4\" style=\"font-family:Arial\"><font rwr=\"1\" size=\"4\" style=\"font-family:Arial\"><font rwr=\"1\" size=\"4\" style=\"font-family:Arial\"><font rwr=\"1\" size=\"4\" style=\"font-family:Arial\"><font rwr=\"1\" style=\"font-family: Arial;\"><font rwr=\"1\" style=\"font-family: Arial;\"><div style=\"font-size: 14pt;\"><br><div style=\"font-size: 14pt;\"><font face=\"Georgia\"><b style=\"font-size: 14pt;\">A Grade 9/10 <\/b><font size=\"4\">- <\/font>this is excellent condition, minor wear or light scratches.<\/font><\/div><div style=\"font-size: 14pt;\"><font face=\"Georgia\"><b style=\"font-size: 14pt;\">B Grade 8/10 <\/b><font size=\"4\">- <\/font>may come with minor discoloration / scratches / minor dings<\/font><\/div><div style=\"font-size: 14pt;\"><font face=\"Georgia\"><b style=\"font-size: 14pt;\">C Grade 6/10<\/b><font size=\"4\"> - <\/font>this is generally for people who'd want to save a lot of money and **doesn't**......
我的代码:
var html = "<iframe srcdoc = '"+Data+"'></iframe>";
当它在“doesn't”中遇到 (') 时终止(在上面的 JSON 字符串中以粗体标记)。
我也试过
var html = '<iframe srcdoc = \"'+Data+"\'><iframe>';
显示一个空的 iframe。此外,当我记录“数据”并在控制台日志中检查时,JSON 字符串中的双引号都没有被转义。
如何让 JSON 文件中的纯文本 html 代码执行并显示在用 javascript 编写的 iframe 中?我希望这个问题有意义。谢谢你。
【问题讨论】:
-
逃跑不行
doesn\'t -
我无法控制那个字符串。它由 JSON 中的 api 调用返回。但是有没有办法让我进入字符串并逃脱它?
-
1) 那不是 JSON 2) 不要使用 iframe。 3)选择一个元素,为该元素创建一个局部变量,我们称它为
ele,然后写成innerHTML就像ele.innerHTML = Data; -
@StevenStark 我希望我不必使用 iframe,但这是学校的作业,规范说我需要。其实是JSON,我这里没有贴整个JSON文件,也没有贴标签的全部值,因为太长了。
-
@Navaneeth 实际上,不,那是 HTML 内容的 URLEncoded 字符串,根本没有 JSON。您可以在以下位置验证什么是有效的 JSON:jsonlint.com
标签: javascript html json iframe