2011-12-06 147 views
1

我正在嘗試爲當前項目創建一個jquery treeview插件。插件可以在這個時候與Firefox運行良好;但是,在使用IE瀏覽器運行時,所有CSS樣式都無法應用。例如,我在css文件#button {...}中定義,然後當我使用id =「button」生成控件時,它無法從css中檢索樣式。我該如何解決這個問題?將css樣式應用於jquery插件

這是我的一些代碼

在js文件

createControls = function() { 

    nDiv = document.createElement("div");  
    nTxt = document.createElement("input"); 
    nTxt.setAttribute('id','txtAdVal'); 
    nTxt.setAttribute('type','text'); 
    nTxt.setAttribute('style' , "width:300px"); 

    nBut = document.createElement("button"); 
    nBut.innerHTML = "Show Active Directory Tree"; 
    nBut.setAttribute("class", "button"); 

    nDiv.appendChild(nTxt); 
    nDiv.appendChild(nBut); 

    $(nBut).bind('click', function(event) { 
     createTree(); 
    }); 

    return nDiv; 
}; 

,並在CSS文件

.button{...} 
+4

您可以發佈您的代碼和相關信息? – Blender

+0

將其編輯到您的問題中。此外,格式化您的代碼。突出顯示它並按下「Ctrl + K」。 – Blender

+0

對不起,對這裏的評論系統不是很熟悉:) –

回答

0

要改變什麼:

nBut.setAttribute("class", "button"); 

要:

nBut.className = "button"; 

既然你已經有包括jQuery的,你也可以確保你正在做的一切跨平臺:

nBut = $("<button class='button'>Show Active Directory Tree</button>"); 
nDiv.append(nBut); 

注意,這需要nDiv是一個jQuery對象(nDiv = $('div'))。

我發現計算器這樣的回答:How can I reliably set the class attr w/JavaScript on IE, FF, Chrome, etc.?

+0

嗯不知何故,我懷疑不使用改變元素的類的jQuery方法將修復任何東西。 – jli

+0

@jli在我開始使用jQuery之前,我使用了'className'並從未遇到過問題。 – Jasper

+0

太棒了!它適用於className屬性。我也有一些地方使用setAttribute('id','...'),它仍然無法使用IE瀏覽器。有id值的解決方法嗎? –