当前位置: 首页 > 面试题库 >

Bootstrap 4文件输入

王经赋
2023-03-14
问题内容

我正在使用bootstrap 4文件浏览器。如果我使用自定义文件控件,则会一直看到“选择文件值”。

选择文件后,我想更改选择文件的值。此值实际上隐藏在CSS中.custom-file- control:lang(en)::after,我不知道如何在javascript中访问和更改它。我可以这样获得所选文件的值:

document.getElementById("exampleInputFile").value.split("\\").pop();

我不需要改变

.custom-file-control:lang(en)::after {
    content: "Choose file...";
}

问题答案:

Bootstrap 4.4

显示选定的文件名也可以使用纯JavaScript完成。这是一个示例,假定带有标签的标准custom-file-input是输入的下一个兄弟元素…

document.querySelector('.custom-file-input').addEventListener('change',function(e){
  var fileName = document.getElementById("myInput").files[0].name;
  var nextSibling = e.target.nextElementSibling
  nextSibling.innerText = fileName
})

Bootstrap 4.1+

现在,在Bootstrap 4.1中,在以下位置设置了“选择文件…”占位符文本 custom-file-label

<div class="custom-file" id="customFile" lang="es">
        <input type="file" class="custom-file-input" id="exampleInputFile" aria-describedby="fileHelp">
        <label class="custom-file-label" for="exampleInputFile">
           Select file...
        </label>
</div>

更改“浏览”按钮文本需要一些额外的CSS或SASS。还要注意使用属性进行语言翻译的方式lang=""

.custom-file-input ~ .custom-file-label::after {
    content: "Button Text";
}

Bootstrap 4 Alpha 6 (原始答案)

我认为这里有2个独立的问题。

<label class="custom-file" id="customFile">
        <input type="file" class="custom-file-input">
        <span class="custom-file-control form-control-file"></span>
</label>

1-如何更改初始占位符和按钮文本

在Bootstrap 4中,使用基于HTML语言的CSS伪元素在上设置初始占位符值。初始文件按钮(实际上不是按钮,但看起来像一个按钮)是使用CSS伪元素设置的。这些值可以用CSS覆盖。custom-file-control``::after``::before

#customFile .custom-file-control:lang(en)::after {
  content: "Select file...";
}

#customFile .custom-file-control:lang(en)::before {
  content: "Click me";
}

2-如何获取选定的文件名值,并更新输入以显示该值。

一旦选择了文件,就可以使用JavaScript / jQuery获得该值。

$('.custom-file-input').on('change',function(){
    var fileName = $(this).val();
})

但是,由于输入的占位符文本是伪元素,因此没有简单的方法可以使用Js/jQuery进行操作。但是,您可以拥有另一个CSS类,一旦选择文件,该类就会
隐藏伪内容

.custom-file-control.selected:lang(en)::after {
  content: "" !important;
}

选择文件后,使用jQuery在.selected类上切换.custom-file-control。这将隐藏初始占位符值。然后将文件名值放入.form-control-file跨度…

$('.custom-file-input').on('change',function(){
  var fileName = $(this).val();
  $(this).next('.form-control-file').addClass("selected").html(fileName);
})

然后,您可以根据需要处理文件上载或重新选择。



 类似资料:
  • 主要内容:Bootstrap4 实例,输入框大小,Bootstrap4 实例,多个输入框和文本,Bootstrap4 实例,复选框与单选框,Bootstrap4 实例,输入框添加按钮组,Bootstrap4 实例,设置下拉菜单,Bootstrap4 实例,输入框组标签,Bootstrap4 实例我们可以使用 .input-group 类来向表单输入框中添加更多的样式,如图标、文本或者按钮。 使用 .input-group-prepend 类可以在输入框的的前面添加文本信息, .input-gro

  • 主要内容:Bootstrap 4 默认设置,<h1> - <h6>,实例,实例,<small>,实例,<mark>,实例,<abbr>,实例,<blockquote>,实例,<dl>,实例,<code>,实例,<kbd>,实例,<pre>,实例,更多排版类Bootstrap 4 默认设置 Bootstrap 4 默认的 font-size 为 16px, line-height 为 1.5。 默认的 font-family 为 "Helvetica Neue", Helvetica, Arial

  • 主要内容:Bootstrap Input,实例,Bootstrap textarea,实例,Bootstrap 复选框(checkbox),实例,实例,Bootstrap 单选框(Radio),实例,实例,Bootstrap select 下拉菜单,实例Bootstrap4 支持以下表单控件: input textarea checkbox radio select Bootstrap Input Bootstrap 支持所有的 HTML5 输入类型: text, password, datet

  • 目录表 文件 使用文件 储存器 储存与取储存 概括 在很多时候,你会想要让你的程序与用户(可能是你自己)交互。你会从用户那里得到输入,然后打印一些结果。我们可以分别使用raw_input和print语句来完成这些功能。对于输出,你也可以使用多种多样的str(字符串)类。例如,你能够使用rjust方法来得到一个按一定宽度右对齐的字符串。利用help(str)获得更多详情。 另一个常用的输入/输出类型

  • 文件 std::fs::File 本身实现了 Read 和 Write trait,所以文件的输入输出非常简单,只要得到一个 File 类型实例就可以调用读写接口进行文件输入与输出操作了。而要得到 File 就得让操作系统打开(open)或新建(create)一个文件。还是拿例子来说明 use std::io; use std::io::prelude::*; use std::fs::File;

  • create 静态方法以只写模式(write-only mode)打开一个文件。若文件已经存在,则旧内容将被销毁。否则,将创建一个新文件。 static LOREM_IPSUM: &'static str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut

  • open 静态方法能够以只读模式(read-only mode)打开一个文件。 File 拥有一个资源,文件描述符(file descriptor),以及在文件丢弃时管理好关闭文件的操作。(原文:A File owns a resource, the file descriptor and takes care of closing the file when it is droped.) use

  • 当使用外部JavaScript库或新的宿主API时,你需要一个声明文件(.d.ts)定义程序库的shape。 这个手册包含了写.d.ts文件的高级概念,并带有一些例子,告诉你怎么去写一个声明文件。 指导与说明 流程 最好从程序库的文档开始写.d.ts文件,而不是代码。 这样保证不会被具体实现所干扰,而且相比于JS代码更易读。 下面的例子会假设你正在参照文档写声明文件。 命名空间 当定义接口(例如: