简体   繁体   English

html5文件输入-如何使用javascript更改文件文本的颜色?

[英]html5 file input - how do I change the color of the file text using javascript?

I'm using an input type=file in a form, and after some javascript validation I'm changing the color to red if invalid and would like to change it back to black if valid. 我使用一种形式的输入type = file,经过一些javascript验证后,如果无效,我将颜色更改为红色,如果有效,则希望将其更改为黑色。

However when I try to change it back to black, it stays red. 但是,当我尝试将其更改回黑色时,它保持红色。 How can I change this text color? 如何更改此文字颜色? Here is an screenshot example of the page after having used javascript to set the color back to black but the filename "2018-06-01-I...-3 cpds.csv" is still red: 这是使用javascript将颜色设置回黑色但文件名“ 2018-06-01-I ...- 3 cpds.csv”仍然为红色后的页面截图示例:

输入颜色重新设置为黑色后文件为红色的文件输入的屏幕截图

Here is my code snippet: 这是我的代码段:

<script>
function validateForm() {
    var dataFileInput = $("#dataFileInput");
    dataFileInput.css("color", "black");
    var dataFiles = dataFileInput[0].files
    console.log("dataFiles:");
    console.log(dataFiles);
    if (0 == dataFiles.length) {
        result = false;
        errorMessagesUl.append('<li style="color: red">please select one or more data files</li>')
        dataFileInput.css("color", "red");
    }
}
</script>
...
<input type=file id=dataFileInput multiple="">

A work around - add a click handler to the file input element to change the color to black when it is clicked: 解决方法-在文件输入元素中添加单击处理程序,以在单击时将颜色更改为黑色:

$(document).ready(function() {
    $("#dataFileInput").click(function(evt) {
        $("#dataFileInput").css("color", "black");
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM