2013-07-24 116 views
0

我試圖重複使用變量「startGame」,通過該變量我聲明一個「a」元素附加到「表」元素,以便測試其自身的存在。我爲此編寫的代碼如下:通過HTML對象獲取子元素

//Helper function to set HTML Tags attributes 
function setAttributes(element, attributes) 
{ for (var key in attributes) { element.setAttribute(key, attributes[key]); } } 

//Opening screen 
var startGame = document.createElement("a"); 
setAttributes(startGame, 
{ 
    "style" : "float:left; width: 100%; text-align: center;", 
    "onclick" : "dealHands()" 
}); 
startGame.appendChild(document.createTextNode("Play")); 

var table = document.getElementById("table"); 
table.appendChild(startGame); 

function dealHands() 
{ 
    if (table.childNodes[0].nodeValue == startGame) 
    { table.removeChild(startGame); } 
    ... 
} 

到目前爲止,代碼無法感知「startGame」並且什麼也沒有發生。

+0

ID爲「table」的元素實際上是一個「

」元素嗎?如果是這樣,那將是問題的一部分。請在問題中加入你的HTML。 –

+0

瀏覽器控制檯中顯示哪些錯誤? –

+0

順便說一句好話**後面**;) –

回答

0

您不能設置樣式屬性使用此:

"style" : "float:left; width: 100%; text-align: center;" 

它們必須單獨設置。但IE不支持以這種方式更改style。您需要:

element.style.cssFloat = "left"; // etc. 

Discussed here

而且,我不會用table作爲ID。

+0

感謝您的建議,我已更改我的IE瀏覽器合規性代碼。 –

0

我只是不得不從if語句中刪除「.nodeValue」,以便比較兩個HTML元素,而不是將值與HTML元素進行比較。