0
我曾經可以將子元素附加到列表動態,但現在出於某種原因JavaScript功能停止工作。此外,錯誤控制檯不會拋出任何錯誤,並且我已經在代碼之間放置了幾個alert('')方法來確保該方法實際上正在運行。這是我曾經有過和它曾經完全正常工作:我無法將子元素附加到列表中
var o = document.getElementById('searchResults');
var li = document.createElement('li'); //creates a list item tag
//if counter is even then highlight the cell different than an odd one
if ((counter) % 2 == 0) {
li.setAttribute('class', 'par');
} else {
li.setAttribute('class', 'non');
}
// I copied the inner html from a row that was displaying. I got rid of the dynamic variables
// just so it is a little easier to understand.
li.innerHTML = "<label class='list3p1'>testing</label><label class='list3p2'>testing2</label><label class='list3p3'>2:23</label>";
o.appendChild(li);
這個功能是工作,但我添加其他表到index.php這個功能停止工作。此外,我在其他頁面中使用了這種類似的方法。還有其他方法正在工作,這是唯一不起作用的功能。
你有'id =「searchResults」'多個元素嗎?這是不允許的,並且可能是在添加額外表格時破壞代碼的原因。 – mVChr 2011-03-28 19:08:46
你添加了什麼代碼?你確定'searchResults'存在並且是'ul'或'ol'元素嗎?而不是使用'setAttribute',使用DOM屬性名稱,如'li.className ='par';'。 – 2011-03-28 19:10:17
你有計數器在其他地方初始化了嗎? – 2011-03-28 19:13:38