2010-12-15 20 views
2

好吧,我有一個jQuery代碼行,我需要轉換爲與Prototype一起工作。原型:檢測Chrome和添加類

$(document).ready(function(){ 
if(navigator.userAgent.indexOf('Chrome')!=-1) 
{ 
    /* Applying a special chrome curosor, 
    as it fails to render completely blank curosrs. */ 
    zoom.addClass('chrome'); 
} 
}); 

zoom是classname,如果檢測到chrome,我想添加class chrome。

到目前爲止原型我有這樣的:

document.observe("dom:loaded", function() { 
Object.prototype.addClass = function(className) { 
    if (!this.hasClass(className)) { //if the class isn't there already 
    this.className += (' ' + className); //append it to the end of the class list 
    } 
} 
}); 

但不幸的是,這是據我可以用谷歌搜索得到。

任何人都有解決方案?

+0

你是什麼意思* zoom是classname *。你是說這是一個jQuery對象,它包含一個具有類「zoom」的元素嗎? – user113716 2010-12-15 03:16:29

+0

這已在下面的帖子中回答。謝謝! – AntoNB 2010-12-16 05:57:37

回答

4

我假設你選擇的類別爲"zoom"

document.observe('dom:loaded', function() { 
    if(navigator.userAgent.indexOf('Chrome')!=-1) { 
     $$('.zoom').each(function(e) { 
      e.addClassName('chrome'); 
     }); 
    } 
}); 

在問題的代碼中,您將添加到Object.prototype。永遠不要這樣做。這隻會導致問題。

+0

你可以用$$('。zoom')減少一些縮進。invoke('addClassName','chrome');' – clockworkgeek 2010-12-15 04:06:36

+0

它非常有效!謝謝一堆! – AntoNB 2010-12-15 05:07:24