2013-10-12 49 views
2

在使用來自Twitter的引導進度欄,我想設置一個不斷變化的變量被送到風格寬度參數:Twitter Bootstrap Style =「width:45%;」

<div class="progress progress-striped"> 
    <div class="bar" style="width: 20%;"></div> 
</div> 

我希望能夠從獲得的百分比數後更改寬度一個數據庫文件。

例如,如果從數據庫中檢索數量爲80我然後摻入80到樣式的百分比部分:

風格=「寬度:80%;」

我已經嘗試在HTML中使用Javascript中的變量,我沒有得到太多。

感謝您的幫助,

湯米

回答

1

希望這個答案有幫助,我喜歡解釋執行你想要的任務的JavaScript和jQuery方法。

jQuery的方法:

$(document).ready(function() { 
    var progWidth = /* The way you'd access the database file to obtain the percent. */ 
    $('.progress-striped > .bar').width(progWidth + "%"); 
}); 

純JavaScript方法:

document.onload = function() { 
    var progWidth = /* The way you'd access the datbase file to obtain the percent. */ 
    $('.progress-striped > .bar').width(progWidth + "%"); 
} 
+0

謝謝...湯米 – TPdeveloper

+1

因爲$('。progress-striped> .bar')是一種jQuery方法,所以我不認爲第二種溶劑是純粹的javascript – cssGEEK

1

把百分比的VAR和JavaScript或jQuery的使用它。我將使用jQuery:

$(document).ready(function() { 
    var theWidth = // Wherever you get the var; 
    $(".progress-striped > .bar").width(theWidth + "%"); 
}); 

要詳細說明「您的var」部分,我們需要知道您是如何獲取數據庫信息的。

+0

FTR,這樣'theWidth'將被解釋爲'px'而不是百分比。 – nietonfir

+0

@nietonfir謝謝。編輯。 –