【问题标题】:How can I change the color of my poll results bar?如何更改投票结果栏的颜色?
【发布时间】: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;
}

【问题讨论】:

  • 我在你的代码中看到了两次&lt;progress id="file",对吗?因为元素 ID 应该是唯一的。并请显示修改颜色的代码(CSS)。
  • 您希望颜色在什么时候完全改变,或者您希望一个是蓝色而另一个是不同的颜色?
  • 可能会觉得这篇文章有帮助:css-tricks.com/html5-progress-element
  • @dalelandry 我希望它们都是另一种颜色(例如粉红色)
  • @RenevanderLende 是的,没错。我将元素 ID 名称更改为 #file1 和 #file2。用于条形颜色更改的 CSS 在我的帖子中 - 但它无法正常工作 - 你可能知道为什么吗?

标签: javascript php html jquery css


【解决方案1】:

实际的进度条由浏览器实现(类似于按钮的方式,如果您之前遇到过 CSS 问题的话),并且使用 CSS 直接修改比仅定位 progress 元素要困难一些.您必须使用特定于供应商的伪元素,例如在 Chrome 中:

#file1::-webkit-progress-value {
    background: pink;
}

我相信 Firefox 所需的 CSS 是:

#file1::-moz-progress-bar {
    background-color: pink;
}

您可以在the MDN documentation 上找到有关您可以应用的其他 CSS 属性(及其供应商特定的伪元素)的更多信息,尤其是在底部附近的“另请参阅”部分。

【讨论】:

  • 非常感谢!这真的很有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
相关资源
最近更新 更多