2012-10-11 22 views
0

我無法使此tabIndex工作。我希望它是第一個元素(它是一個完成按鈕,但不是「按鈕」類型),當我點擊它進入的標籤。實際上,當我給它a.tabIndex = 1;它會做相反的處理,並在正常/默認的Tab鍵流程中完全跳過。爲什麼是這樣?Javascript tabIndex = 1給出相反的效果...元素被完全跳過

var div = document.createElement("div"); 
    div.setAttribute("class", "container"); 
    div.setAttribute("id", "doneCon"); 
    div.style.width = "70px"; 
    div.style.height = "35px"; 
    div.style.overflow = "hidden"; 
    div.style.borderStyle = "solid"; 
    div.style.borderColor = "blue"; 
    div.style.borderRadius = "35px/18px"; 
    div.style.backgroundColor = "white"; 
    div.style.marginLeft = "5px"; 
    div.style.display = "inline-block"; 
    var a = document.createElement("a"); 
    a.setAttribute("href", "#"); 
    a.setAttribute("class", "a container"); 
    a.tabIndex = 1 
    a.setAttribute("id", "done"); 
    a.style.display = "block"; 
    a.style.fontFamily = "yo2"; 
    a.style.weight = "bold"; 
    a.style.fontSize = "150%"; 
    a.style.paddingTop = "7px"; 
    a.style.textDecoration = "none"; 
    a.style.color = "blue"; 
    a.style.textAlign = "center"; 
    text = document.createTextNode("Done"); 
    a.appendChild(text); 
    div.appendChild(a); 
+0

你知道tabIndex行在最後沒有分號嗎?如果這與您的文件不同,我會在指出問題之前更正此副本。 –

+0

[Worksforme](http://jsfiddle.net/FV2yY/embedded/result/) – Bergi

+0

您在JavaScript中設置所有這些樣式屬性的原因,而不僅僅是CSS?它會縮減代碼並使其更具可讀性/可維護性。 – epascarello

回答

0

我想你需要使用appendChild adiv您設置tabIndex之前。通過這個動作來保持tabIndex是不太可能的,因爲你將anchor(a)附加到div的子節點的末尾。

爲了找到答案,在appendChild後面加上一個斷點,並在append後面查看tabIndex屬性的值(使用firebug或Chrome內置的開發者工具等效項)。