2015-06-06 120 views
0

我有一個CSS這是由jQuery的實施問題,CSS是在Mozilla工作正常,但無法在Chrome所以需要根據瀏覽器,但在jQuery來使CSS一個變化如何使用jQuery檢測瀏覽器?

任何人幫我弄瀏覽器是否是Firefox,Chrome或其他。

if ($browserFirefox) { 
    ....code 
    $('#test').css("margin-top","10%"); 
} 
if ($browserChorme) { 
    code goes here 
} 
if ($browserXYZ) { 
    code goes here 
} 
+2

請教,而不是質疑有關'CSS是在Mozilla的工作很好,但不是在CH rome'? 'margin-top:10%是什麼問題;'? –

+1

可能重複的[JavaScript中的瀏覽器檢測?](http://stackoverflow.com/questions/2400935/browser-detection-in-javascript) – GillesC

+0

可能的重複http://stackoverflow.com/questions/9847580/how-以檢測Safari瀏覽器Chrome瀏覽器即Firefox和Opera瀏覽器 –

回答

0

您可以使用$.browser

if($.browser.mozzila){ 
} 

但是,我強烈建議您嘗試使用跨瀏覽器的解決方案。用戶代理嗅探不是一個推薦的方法,因爲它可以很容易中斷(想象同一瀏覽器的多個版本)。

Downsides of using the navigator object/user-agent sniffing for detecting IE versionsBrowser Detection (and What to Do Instead)

更新 的jQuery> 1.9

此屬性的jQuery 1.9移除,只可通過 的jQuery.migrate插件。請嘗試使用來代替

+1

不,這已被刪除jq 1.9 –

+0

@ A.沃爾夫,謝謝你的擡頭 – AmmarCSE

0

你可能會在你的css而不是用腳本解決問題。查看http://browserhacks.com/瞭解瀏覽器的特定定位信息。您還可以使用jQuery把一個特定的類就可以了,但隨後在你的CSS

0

此不同的申報物品可以與jQuery.browser property

if ($.browser.msie) { 
    ....code 
    $('#test').css("margin-top","10%"); 
} 
if ($.browser.webkit) { 
    code goes here 
} 
if ($.browser.mozilla) { 
    code goes here 
} 

實現應該做的工作。

0

如果您需要做的CSS變化與differente瀏覽器,你可以按照打擊碼容易

if Webkit (Safari/Chrome) { 

#myDIV {margin-top:-3px} 

} else if (Firefox) { 

#myDIV {margin-top:0px} 

} else { // IE and other browsers 

    #myDIV {margin-top:1px} 
} 

雖然在jQuery中的水平,你可以使用jquery.browser.Take看看這個 http://api.jquery.com/jquery.browser/

5

使用navigator.userAgent的瀏覽器爲信息,如:

if (navigator.userAgent.search("MSIE") >= 0) { 
    //code goes here 
} 
else if (navigator.userAgent.search("Chrome") >= 0) { 
    //code goes here 
} 
else if (navigator.userAgent.search("Firefox") >= 0) { 
    //code goes here 
} 
else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) { 
    //code goes here 
} 
else if (navigator.userAgent.search("Opera") >= 0) { 
    //code goes here 
}