2

好的,我有一行 jQuery 代码,我需要将其转换为使用 Prototype。

$(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 是类名,如果检测到 chrome,我想将类 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
    }
 }
});

但不幸的是,这是我通过谷歌搜索所能得到的。

有人有解决方案吗?

4

1 回答 1

4

我假设您正在选择带有 class 的元素"zoom"

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

在问题的代码中,您要添加到Object.prototype. 永远不要那样做。它只会引起问题。

于 2010-12-15T03:34:15.810 回答