2013-03-29 60 views
0

我不明白爲什麼如果我試圖返回CSS邊界屬性,它會返回一個空字符串,即使我已經設置了CSS的邊框。我也嘗試使用內聯方法設置邊框樣式,但仍然無法工作..任何想法?謝謝!jQuery插件 - 如果沒有選項填充,返回一個css屬性

(function($){ 

    var methods = { 
    init : function(options, callbacks) { 
     if(options === undefined) { 
      return this.css("border"); 
     } 

     return this.each(function(){ 
      if(options === null) { 
       $(this).css("border","none"); 
       return; 
      } else if(typeof options === "string") { 
       $(this).css("border", options); 
       return; 
      } else if(typeof options === "object") { 

       if(typeof options.style !== "undefined") { 
        $(this).css("border-style", options.style); 
       } else { 
        $(this).css("border-style", "solid"); 
       } 

       if(typeof options.width !== "undefined") { 
        $(this).css("border-width", options.width); 
       } 

       if(typeof options.color !== "undefined") { 
        $(this).css("border-color", options.color); 
       } 

       if(typeof options.radius !== "undefined") { 
        $(this).css("border-radius", options.radius); 
       } 
      } 
     }); 
    } 
}; 

$.fn.border = function(method) { 
    if (methods[method]) { 
     return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 
    } else { 
     return methods.init.apply(this, arguments); 
    } 
}; 

})(jQuery); 

如果我試圖返回元素的顏色..一切似乎沒問題..但沒有與邊界屬性。

+0

好像我在最新的谷歌瀏覽器的工作,雖然我在的jsfiddle作爲函數設置它,但它應該爲你工作不管。 [** FIDDLE **](http://jsfiddle.net/DJDavid98/BR95Y/)。 – SeinopSys

+0

謝謝DJDavid ..我用另一種方法來解決這個問題..我會把它作爲這個問題的答案 –

回答

0

我已經解決了這個問題是這樣的:

(function($){ 

    var methods = { 
     init : function(options, callbacks) { 

      return this.each(function(){ 
         if(options === null) { 
        $(this).css("border","none"); 
       } else if(typeof options === "string") { 
        $(this).css("border", options); 
       } else if(typeof options === "object") { 

           if(typeof options.style !== "undefined") { 
            $(this).css("border-style", options.style); 
           } else { 
            $(this).css("border-style", "solid"); 
           } 

        if(typeof options.width !== "undefined") { 
         $(this).css("border-width", options.width); 
        } 

        if(typeof options.color !== "undefined") { 
         $(this).css("border-color", options.color); 
        } 

        if(typeof options.radius !== "undefined") { 
         $(this).css("border-radius", options.radius); 
        } 
       } 
      }); 
     }, 
      getBorderStyle: function(options, callback) { 

        var elements = new Array(); 

        this.each(function(){ 

         var borderProperties = {}; 

         borderProperties.style = this.style.borderStyle; 
         borderProperties.color = this.style.borderColor; 
         borderProperties.width = this.style.borderWidth; 
         borderProperties.radius = this.style.borderRadius; 
         borderProperties.element = this; 
         elements.push(borderProperties); 
        }); 

        return elements; 
      } 
    }; 

    $.fn.border = function(method) { 
      if (methods[method]) { 
       return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 
      } else if(method === undefined) { 
       return methods["getBorderStyle"].apply(this, arguments); 
      } else { 
       return methods.init.apply(this, arguments); 
      } 
    }; 

})(jQuery); 
相關問題