2013-07-19 68 views
0

由於jQuery .css()不工作,如果我想獲得的css值marginpaddingjQuery - 擴展css()

我找到了解決這個bug的jquery代碼。

請幫我extednd jQuery的.css()通過添加以下代碼:

$.each(['border', 'margin', 'padding'], function (i, name) { 
     $.fn[name] = function (value) { 
      if (value) { 
       if (value.top !== undefined) { 
        this.css(name + '-top' + (name === 'border' ? '-width' : ''), value.top); 
       } 
       if (value.bottom !== undefined) { 
        this.css(name + '-bottom' + (name === 'border' ? '-width' : ''), value.bottom); 
       } 
       if (value.left !== undefined) { 
        this.css(name + '-left' + (name === 'border' ? '-width' : ''), value.left); 
       } 
       if (value.right !== undefined) { 
        this.css(name + '-right' + (name === 'border' ? '-width' : ''), value.right); 
       } 
       return this; 
      } 
      else { 
       return {top: num(this.css(name + '-top' + (name === 'border' ? '-width' : ''))), 
         bottom: num(this.css(name + '-bottom' + (name === 'border' ? '-width' : ''))), 
         left: num(this.css(name + '-left' + (name === 'border' ? '-width' : ''))), 
         right: num(this.css(name + '-right' + (name === 'border' ? '-width' : '')))}; 
      } 
     }; 
    }); 

HRE是這表明jQuery的.css()失敗填充和保證金中獲得價值,在Firefox瀏覽器中的小提琴

http://jsfiddle.net/howtoplease/fMTsW/4/

+1

你在問什麼?你有沒有嘗試過這些代碼?它沒有工作或什麼? – Paulpro

+0

更新了我的問題 – user007

+0

是在Chrome中工作,但在Firefox中失敗,我想用上面的代碼來擴展'css()'功能 – user007

回答

1

我有一個解決方案,看看這個。

$(document).ready(function(){ 
     .... 
      $('[name='+this.name+']').val(marginT); 
      ..... 
     }else{   
     this.value = $('h3').css(this.name); 
     } 
    }); 
}); 

WORKING DEMO

上面的代碼是我自己的邏輯, 我還實施了您所提供的代碼。

Your Plugin function

這個插件也有問題,檢查顯示的console.log。

+0

Hoory !!加工。謝謝 – user007

+0

如果margin:40px 50px; – user007