开发者

Prototype: Detect Chrome and Add Class

开发者 https://www.devze.com 2023-01-30 10:00 出处:网络
Okay, I have a jQuery line of code, that I need to convert to work with Prototype. $(document).ready(function(){

Okay, I have a jQuery line of code, that I need to convert to work with 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 is the classname and I want to add the class chrome to it if chrome is detected.

So far for Prototype I have this:

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
    }
 }
});

But unfortunately, that's as far as I can get with Google searching.

Anyone have a solution?


I'll assume you're selecting elements with the class "zoom".

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

In the code in the question, you're adding to Object.prototype. Never do that. It will only cause problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消