【发布时间】:2020-06-09 01:15:34
【问题描述】:
我正在使用两个结果栏进行民意调查。结果栏都是蓝色的,但是当我尝试更改栏颜色时,它不会改变。
我已尝试查看有关堆栈溢出的其他答案,(How can I change the color of a progress bar using javascript?:) 但它们似乎不起作用。
<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/study_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
<div id="poll">
<h2>When studying, do you often find yourself procrastinating?</h2>
<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>
</script>
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);
$yesProgress = 100*round($yes/($no+$yes),2);
$noProgress = 100*round($no/($no+$yes),2);
?>
<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<progress id="file1" max="100" value="<?= $yesProgress ?>">
<?= $yesProgress ?>
</progress>
<?= $yesProgress ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<progress id="file2" max="100" value="<?= $noProgress ?>">
<?= $noProgress ?>
</progress>
<?= $noProgress ?>%
</td>
</tr>
</table>
下面尝试将条形颜色从蓝色更改为粉红色的 CSS 代码不起作用。绿色和灰色条是我尝试更改为粉红色的条。结果如下图:
CSS:
#poll {
text-align: center;
background-color: #FFDFB2;
border-radius: 2vw;
padding: 10px 50px;
display: inline-block;
}
progress#file[value]::-webkit-progress-bar {
background-color: purple;
border-radius: 2px;
}
【问题讨论】:
-
我在你的代码中看到了两次
<progress id="file",对吗?因为元素 ID 应该是唯一的。并请显示修改颜色的代码(CSS)。 -
您希望颜色在什么时候完全改变,或者您希望一个是蓝色而另一个是不同的颜色?
-
可能会觉得这篇文章有帮助:css-tricks.com/html5-progress-element
-
@dalelandry 我希望它们都是另一种颜色(例如粉红色)
-
@RenevanderLende 是的,没错。我将元素 ID 名称更改为 #file1 和 #file2。用于条形颜色更改的 CSS 在我的帖子中 - 但它无法正常工作 - 你可能知道为什么吗?
标签: javascript php html jquery css