2014-05-02 44 views
1

我有以下jQuery代碼。jQuery的CSS屬性頂部和左不工作

if(bubble.label != undefined){ 
         console.log(bubble.label.attr('style')); 
         var bubbleStyle = bubble.label; 
         bubbleStyle.css({ 
         color: 'red', 
         left: 0+'px', 
         top: -660+'px' 
        }); 
} 

在上面的代碼CSS屬性顏色:'紅',適用,但不是左和頂。我已經嘗試過給位置:'絕對',但它仍然不起作用。請幫助我。謝謝。

+0

選擇器可能有問題。提供完整的腳本和HTML或創建一個小提琴。 – theHarsh

回答

1

嘗試使用:的

left: 0, 
top: -660 

代替:

left: 0+'px', 
top: -660+'px' 
+0

左邊不能使用:0, top:-660。 –

+0

控制檯中是否有錯誤?你能設置一個演示來複制你的問題嗎? – Felix

+0

不,沒有錯誤,我可以看到顏色屬性的變化,但不是頂部。 –

0

使用像this.Please確保position:absolute or relative在CSS設置。那麼只有它的工作原理,否則它不工作

left: '0px', 
top: '-660px' 
+0

我試過這個,不行。 –

+0

@downvoter請解釋我做了什麼錯誤? –

+0

沒有錯誤,但這並不能解決我的問題。 –

1

變化左邊和頂部:

left: '0px', 
top: '-660px' 
+0

我試過這個,不行。 –

+0

請確保在css中設置絕對或相對位置user3118011 –

0

嘗試使用這樣的:

bubbleStyle.css({ 
    color: "red", 
    left: "0", 
    top: "-660px" 
    }); 

此外,檢查元素,看內嵌風格已被應用。

+0

不適用於此。 –

+0

它應該工作http://jsfiddle.net/sSr6z/ 確保頁面上存在氣泡元素。此外,請注意頂部爲負值,因此該元素可能在頁面上不可見。 –

0

考慮bubble.label是一個HTML元素label嘗試以下方法:

<label id="test">Testlabel</label> 

確保顯示inline-block,並具有relative定位label

#test { 
    display: inline-block; 
    position: relative; 
    background-color: green; 
    color: white; 
} 

var bubbleStyle = jQuery('#test'); 

bubbleStyle.css({ 
    color: 'red', 
    backgroundColor: 'orange', 
    left: 0, 
    top: 660, 
}); 

See the fiddle

0

使用jQuery的.css功能更新前值,但更改不會反映在屏幕上。

使用bubbleStyle.offset({top:-660});它將更新DOM和屏幕中的最高值。