正确打印 Javascript 数组值

Print Javascript Array Value Properly

所以我有一个 javascript 数组,在数组中我有这个:

var quiz = [{
    "question": "What is the greatest common factor of 105 and 126?",
    "image": "",
    "choices": ["3", "7",
        "9", "21", "35"
    ],
    "correct": "21",
    "conceptlink": "https://www.khanacademy.org/math/in-sixth-grade-math/playing-numbers/highest-common-factor/v/greatest-common-divisor",
    "explanation": "In order to solve this question, you must factor each number into prime factors<h1>Height</h1>",
}];

关注数组的解释子集。我有一个要打印的 H1 元素,但它是作为文本而不是代码的一部分打印的。换句话说,文本将显示为:

而不是:

这是打印解释的代码

 $('#explanation').html('<strong>Incorrect.</strong> ' + htmlEncode(quiz[currentquestion]['explanation']));

非常感谢 js 专家的任何帮助!!

你快搞定了。我不确定你的函数 htmlEncode() 做了什么,但如果你不使用它,它应该会给你你想要的。这是一个演示它的 jsbin:http://jsbin.com/yeyuqoqabe/edit?html,output

如果您想知道如何使字符串 HTML 安全,这里有一个问题可以提供帮助:How to encode a string in JavaScript for displaying in HTML?

您可以使用 jQuery 对文本进行转义。

var escaped = $("<div>").text(quiz[0]['explanation']).html();

https://jsfiddle.net/ouu6x2ge/

我认为您不需要 htmlEncode。我建议你试试:

$('#explanation').html('<strong>Incorrect.</strong><span>').find('span').text(quiz[currentquestion]['explanation']))