【发布时间】:2020-06-08 05:54:39
【问题描述】:
我正在尝试使用两个粉红色条进行民意调查,以显示结果的百分比。我制作了百分比条形图像,尽管当我使用民意调查时,它们的大小保持不变,并且根据它们的百分比不会变得更小/更大。我应该如何更改代码以使 % 条改变它们的长度?
HMTL 代码:
<html>
<head>
<script>
function getVote(int) {
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("poll").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","poll_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="poll">
<h3>Do you like PHP and AJAX so far?</h3>
<form>
Yes: <input type="radio" name="vote" value="0" onclick="getVote(this.value)"><br>
No: <input type="radio" name="vote" value="1" onclick="getVote(this.value)">
</form>
</div>
</body>
</html>
PHP 文件:
<?php
$vote = $_REQUEST['vote'];
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);
//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];
if ($vote == 0) {
$yes = $yes + 1;
}
if ($vote == 1) {
$no = $no + 1;
}
//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td><img src="images/poll.png"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td><img src="images/poll.png"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>
【问题讨论】:
-
您正在使用图像。您希望图像如何根据百分比调整大小?使用 CSS 来做到这一点。试试这个:developer.mozilla.org/en-US/docs/Web/HTML/Element/progress 在 CSS 框架中也有一些选项,比如 Bootstrap。 getbootstrap.com/docs/4.0/components/progress
-
对不起,我怎么能用 CSS 做到这一点?你认为我可以使用 gif 吗?
-
没有。参考以上链接。
标签: javascript php html jquery css