2016-08-18 71 views
-1

所以我有一个JavaScript数组,数组中我有这样的:在阵列的解释打印的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元素,但是,它被打印为文本的一部分而不是代码。换句话说,文本内容如下:

enter image description here

相反的: enter image description here

下面是打印解释从JS专家将很乐意

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

任何帮助代码感谢!

+0

'htmlEncode()'似乎工作正常:) – Andreas

+0

不幸的是。我多次测试过它。 :( –

回答

0

你几乎拥有它。我不确定你的函数htmlEncode()是做什么的,但是如果你不使用它,它应该给你你想要的。这里有一个jsbin证明它:http://jsbin.com/yeyuqoqabe/edit?html,output

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

+0

谢谢,我认为htmlEncode原生Jquery,我找到了源代码并编辑了这个函数! –

0

我不认为你需要的HTMLEncode了点。我建议你尝试:

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