2011-05-03 66 views
0

我使用以下策略將HTML標籤動態插入到文檔中。它適用於Mac Chrome和Firefox,但不適用於Windows IE或Windows Firefox。我懷疑這與分配給新創建的div標記的innerHTML屬性的字符串中的尖括號有關。有任何想法嗎?動態標籤插入無法在Windows IE或Firefox上工作

function addElement() { 

     var ni = document.getElementById('stateSet'); 
     var newdiv = document.createElement('div'); 
     var divIdName = 'state'+ stateCount; 
     stateCount++; 
     newdiv.setAttribute('id',divIdName); 
     newdiv.innerHTML = '<input id="name" type="text"/> <input id="setting" type="text"/><button type="button" >x</button>' 
     ni.appendChild(newdiv); 

    } 
+0

你不需要的按鈕元素 – Neal 2011-05-03 16:48:07

+0

一個'type'其中'stateCount'定義? – Neal 2011-05-03 16:49:20

回答

0

嘗試宣告statecount

function addElement() { 
    var stateCount = 0; 
    var ni = document.getElementById('stateSet'); 
    var newdiv = document.createElement('div'); 
    var divIdName = 'state'+ stateCount; 
    stateCount++; 
    newdiv.setAttribute('id',divIdName); 
    newdiv.innerHTML = '<input id="name" type="text"/> <input id="setting" type="text"/><button type="button" >x</button>' 
    ni.appendChild(newdiv); 

} 

http://jsfiddle.net/TPx3v/

相關問題