问题
My code is designed to set the width of my progress bar by calculating the percentage and passing it as a style.width. I am a novice, so apologise for the bad code:
JQuery
$(document).ready(function() {
var width=(1/5*100);
$('#progress_bar').css('width','=width + "%"');
});
HTML
<div id="progress_bar" style="height:1em; background:red; display:block;"></div>
Could someone with a second to spare please help me to get it working and show me where I've gone wrong so I can learn from this?
http://jsfiddle.net/SyxAM/
回答1:
The string '=width + "%"'
can't be the value of a css parameter.
You probably wanted
$('#progress_bar').css('width', width + "%");
回答2:
this will solve your problem
var width=(1/5*100);
$('#progress_bar').css('width',width + "%");
回答3:
Your variable appending was wrong.It should be like this;
$(document).ready(function() {
var width=(1/5*100);
$('#progress_bar').css('width', width + "%");
});
You can see here http://jsfiddle.net/SyxAM/2/
来源:https://stackoverflow.com/questions/11947220/progress-bar-width