2016-08-18 107 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']))