如何使用Bootstrap4和ES6创建自定义确认框
by Prashant Yadav
通过Prashant Yadav
如何使用Bootstrap4和ES6创建自定义确认框 (How to create a custom confirm box with Bootstrap4 and ES6)
We put lots of sweat into achieving a good design, but imagine a scenario where we have to use something which is styled default to browsers. It ruins our hard work as it changes browser to browser.
我们为实现一个好的设计付出了很多汗水,但是想象一个场景,我们必须使用浏览器默认样式的东西。 更改浏览器会破坏我们的辛苦工作。
Same happened with me: I had put lots of effort into creating a SPA but the client wanted a confirm box with business logic. Unfortunately, using the inbuilt confirm box was the only odd factor in the design. So I created a custom confirm box.
我也发生了同样的事情:我在创建SPA方面付出了很多努力,但是客户想要一个带有业务逻辑的确认框。 不幸的是,使用内置的确认框是设计中唯一的奇怪因素。 因此,我创建了一个自定义确认框。
Let us see how you can create your own custom confirm box with Bootstrap4 and ES6.
让我们看看如何使用Bootstrap4和ES6创建自己的自定义确认框。
A modal is a popup dialog that is available with Bootstrap. We can use its functionality to handle the popup and design it according to our needs. It has different methods which we can use to achieve our goal.
模态是Bootstrap可用的弹出对话框。 我们可以使用其功能来处理弹出窗口并根据需要进行设计。 它具有可用于实现目标的不同方法。
Check out the live demo here.
在此处查看现场演示。
- We are going to use Bootstrap4 modal for creating the popup dialog.我们将使用Bootstrap4模式创建弹出对话框。
We will use the Javascript callback function to handle the response.
我们将使用Javascript 回调函数来处理响应。
- As Bootstrap has a JQuery dependency, we will use it for events.由于Bootstrap具有JQuery依赖关系,因此我们将其用于事件。
引导模态方法 (Bootstrap Modal Methods)
.modal(options): Activates the content as a modal with the below options.
.modal(options):使用以下选项将内容激活为模态。
.modal(“toggle”): Shows or Hides the modal based on its current state.
.modal(“ toggle”):根据其当前状态显示或隐藏模态。
.modal(“show”): Opens the modal.
.modal(“ show”):打开模态。
.modal(“hide”): Hides the modal.
.modal(“ hide”):隐藏模态。
引导程序模式选项 (Bootstrap Modal Options)
{backdrop: true or false, default: true}: Should modal hide when clicked outside the modal.
{backdrop:是或否,默认值:true}:在模态外部单击时,模态应隐藏。
{ keyboard: true or false, default: true}: Should modal hide when a keyboard button is pressed.
{keyboard:true或false,默认值:true}:按下键盘按钮时是否应该模式隐藏。
{ show: true or false, default: true}: To show the modal when initialized.
{show:true或false,默认:true}:初始化时显示模态。
自举模式事件 (Bootstrap Modal Events)
.on(‘show.bs.modal’): When modal is about to be shown but it is not yet shown.
.on('show.bs.modal'):模态将要显示但尚未显示时。
.on(‘ shown.bs.modal’): When modal is shown.
.on('shown.bs.modal'):显示模态时。
.on(‘ hide.bs.modal’): When modal is about to be hidden but it is not yet hidden.
.on('hide.bs.modal'):模态将要隐藏但尚未被隐藏时。
.on(‘ hidden.bs.modal’): When modal is hidden.
.on('hidden.bs.modal'):模态隐藏时。
依存关系 (Dependencies)
Import all the dependencies using CDN.
使用CDN导入所有依赖项。
We will now create a function that will generate our confirm box with our custom message and return the output selected by the user.
现在,我们将创建一个函数,该函数将生成带有自定义消息的确认框,并返回用户选择的输出。
We don’t need the header and footer of the modal. We will just use the modal body and display our message in an <h4> tag. Then we will create two buttons below it with different classes b
tn-yes and
btn-no which will be used to handle the response.
我们不需要模态的页眉和页脚。 我们将只使用模式主体并将消息显示在<h4>标记中。 然后我们将创建它下面两个按钮具有不同的cl asses b
TN业s and
BTN-没有将用于处理响应。
说明 (Explanation)
We have created a
function
which accepts amessage
and a callbackhandler
.我们创建了一个
function
,该function
接受message
和回调handler
。In this, we are appending the modal at the end of the
body
tag with JQuery’sappendto
method.在这种情况下,我们使用JQuery的
appendto
方法在body
标签的末尾附加模式。After appending the modal, we are triggering or showing the modal with Bootstrap’s
modal
method. We are also passing two parameters{backdrop: 'static', keyboard:false}
which prevent the modal from hiding with keyboard events, like pressing the ESC button or clicking in the backdrop area.附加模态后,我们将使用Bootstrap的
modal
方法触发或显示模modal
。 我们还传递了两个参数{backdrop: 'static', keyboard:false}
,可以防止模式隐藏在键盘事件中,例如按ESC按钮或单击背景区域。We are checking if the modal is hidden with bootstrap’s
hidden.bs.modal
and then we are removing the modal with JQuery’sremove
method. This will prevent it from adding a modal every time.我们正在检查是否使用bootstrap的
hidden.bs.modal
隐藏了模态,然后使用JQuery的remove
方法删除了模态。 这样可以防止它每次都添加模态。
We have created a function that will render and open our custom confirm box, so now we have to handle the response provided by the user.
我们创建了一个函数,该函数将呈现并打开我们的自定义确认框,因此现在我们必须处理用户提供的响应。
We will use a callback function to handle the response. In JavaScript, we can pass the function as an argument to another function and call it. The functions passed as arguments are called the callback functions.
我们将使用回调函数来处理响应。 在JavaScript中,我们可以将该函数作为参数传递给另一个函数,然后调用它。 作为参数传递的函数称为回调函数。
处理回应 (Handling the response)
说明 (Explanation)
If yes or no button is pressed then we pass true
or false
to the callback function and call it. After that, we hide the modal.
如果按下是或否按钮,则将true
或false
传递给回调函数并调用它。 之后,我们隐藏模态。
We can call our confirm box like this wherever we want.
我们可以在任何地方调用这样的确认框。
Check out the live demo here.
在此处查看现场演示。
With a custom confirm box, we have complete control over the design and some control over the functionality.
使用自定义确认框,我们可以完全控制设计并控制某些功能。
If you liked this article, please give it some ?and share it! If you have any questions related to this feel free to ask me.
如果您喜欢这篇文章,请给它一些?并分享! 如果您对此有任何疑问,请随时问我。
For more like this and algorithmic solutions in Javascript, follow me on Twitter. I write about ES6, React, Nodejs, Data structures, and Algorithms on learnersbucket.com.
有关Java的更多此类信息和算法解决方案,请在 Twitter上 关注我 。 我写ES6 ,React过来,的NodeJS, 数据结构和算法上learnersbucket.com 。
翻译自: https://www.freecodecamp.org/news/custom-confirm-box-with-bootstrap-4-377aa67723c2/
相关文章:

RequireJS学习笔记(转)
前言进入移动前端是很不错的选择,这块也是我希望的道路,但是不熟悉啊。。。现在项目用的是requirebackbone,整个框架被封装了一次,今天看了代码搞不清楚,觉得应该先从源头抓起,所以再看看require了。上午是…

火狐中的table
在火狐浏览器中,table的th、td会存在小数转载于:https://www.cnblogs.com/likwin/p/7270817.html

React路由 react-router-dom
React的路由: 首先我们创建一个React应用 npm install -g create-react-app create-react-app demo-app cd demo-app安装路由 npm install react-router-dom npm add babel/runtime 接下来,将以下任一示例复制/粘贴到中src/App.js。 第一个示例&…

编程基础 垃圾回收_为什么我回收编程问题
编程基础 垃圾回收by Amy M Haddad通过艾米M哈达德(Amy M Haddad) 为什么我回收编程问题 (Why I Recycle Programming Problems) Many programmers are given the same advice: solve as many problems as possible. It’s true that solving new problems can help you gain …

SQL Server孤立账户解决办法
选择你想要的数据库。 执行 exec sp_change_users_login UPDATE_ONE,用户名,登录名 假如你的登录名是:sd exec sp_change_users_login UPDATE_ONE,sd,sd 转载于:https://www.cnblogs.com/runliuv/p/7273675.html

vue更新data无效,页面data没刷新 vue.set
Vue中组件的data是有很多坑的,先普及一下常识: 1.想使用data,必须先在data中创建。(如果不创建就会报错)示例: <div class"">{{user.Age}}</div>new Vue({el: #app,data: {user:{A…

重温Thinking in java
1、高精度 BigInteger、BigDecimal 支持任意大小的数字 不能使用运算符 运算速度相对于int、float稍慢 2、对象作用域 {String s new String("aaa"); } 在}外 此时栈中的引用s已经超出了自己的作用域 便不存在了 但是new String("aaa")这个堆中的对象仍然…

2018 react 大会_React Conf 2018的经验教训
2018 react 大会by Yangshun Tay阳顺泰 React Conf 2018的经验教训 (Lessons Learned at React Conf 2018) I was fortunate to have attended React Conf 2018 thanks to my managers — it was an awesome event. I have been watching past React Conf videos online and i…

删除url中某个参数
这里的url 是指一个网站链接 例如: https://baidu.com?a1&b2 下面看一下封装的代码 <!DOCTYPE html> <html><head><meta charset"utf-8"><script src"https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js&q…

【转载】MSXML应用总结 开发篇(下)
原文:http://blog.sina.com.cn/s/blog_48f93b530100eq4b.html 三、查询XML文档节点 这部分属于“读”XML文档并做节点遍历,由于担心加上实例会占用过多的篇幅影响阅读,先在这篇做方法总结,以后有时间再写一篇“实战篇”专门写个实…

d010:盈数、亏数和完全数
题目: 对一个正整数N而言,将它除了本身以外所有的因子加起来的总和为S,如果S>N,则N为盈数,如果S<N,则N为亏数,而如果SN,则N为完全数(Perfect Number)。例如10的因子…

软件开发 理想_我如何在12个月内找到理想的软件工作
软件开发 理想In this talk, Matt Woods shares the 3 cornerstone habits that helped him land his dream software job in just 12 months. These habits paved the way for him to consistently grow as a software developer, balloon his professional network, and easi…

Angular4.0从入门到实战打造在线竞拍网站学习笔记之四--数据绑定管道
Angular4.0基础知识之组件Angular4.0基础知识之路由Angular4.0依赖注入Angular4.0数据绑定&管道 数据绑定 数据绑定允许你将组件控制器的属性和方法与组件的模板连接起来,大大降低了开发时的编码量。 常见的表现形式有: 插值表达式:<h…
给GridView增加求和行
1、在WebForm窗体中,设置GridView的ShowFooter"True" 2、在后台代码中 private int jhrs0,ybrs0;//定义变量 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowIndex > 0) { jhrs Convert.ToInt16(e.Ro…

phpMyAdmin 数据库添加int类型的值时默认设为唯一主键的问题解决
数据库的表中插入了一条数据,再插入数据就插入不进去。 这是我今天在开发数据库的时候,遇到一个问题,经过排查,是数据库的结构有问题,有字段是唯一数据,但是目前还不想设置它的值。 场景环境描述ÿ…

python 类中定义类_Python中的动态类定义
python 类中定义类Here’s a neat Python trick you might just find useful one day. Let’s look at how you can dynamically define classes, and create instances of them as required.这是一个整洁的Python技巧,有一天可能会有用。 让我们看一下如何动态定义…

用自定义方法,传入成绩数组,实现输出考试成绩的成三名
package com.imooc; import java.util.Arrays; //导入Array类包 import java.util.Scanner; //导入Scanner类包 public class Final2 { public static void main(String args[]){Scanner…

EJS 什么是EJS后缀文件 EJS怎么用
一、什么是EJS EJS是一个JavaScript模板库,用来从JSON数据中生成HTML字符串。 二、为什么要使用EJS 与最初的JavaScript相比较,一些不太了解你的代码的人可以更容易地通过EJS模板代码看得懂你的代码。 让我们放松一下,一起来享受下令人激动的…

一个推荐系统,实现完整的设计-在百度搜索关键词推荐案例
在之前一篇博文中, 有同学在评论中问了个问题: 怎样解决因式分解带来的推荐冷门。热门关键词的问题。 在回答这个问题的时候, 想到了近几年在做搜索推荐系统的过程中, 学术界和工业界的一些差别。 正好近期正在做技术规划…

如何充分利用JavaScript(ES6)中的解构功能
by Joanna Gaudyn乔安娜高登(Joanna Gaudyn) Destructuring was a new addition to ES6. It took inspiration from languages like Python and allows you to extract data from arrays and objects into distinct variables. It might sound like something you’ve done in…

React 开发环境搭建
先来一个 React 官方文档的链接 点击跳转 搭建 React 的前期准备:你的本地环境需要安装 cnpm、node。 注:代码块中的 $ 代表: $后面是在命令行输入的命令,举例 $ npm start 解:实际上是应该打开命令行输入 npm st…

c++中的友元重载
1 语法 返回值类型 operator 运算符名称(形参列表) { 重载实体 } --------->operator和运算符名称在一起构造成新的函数名 2 案例 1 #include <iostream>2 3 using namespace std;4 5 class Complex6 {7 public:8 9 Complex(float x0,float y0) 10 :_…

WPF解析TTF 字体
偶遇需要自己解析 TTF 字体并显示,此做。。。 using System; using System.Collections.Generic; using System.Drawing.Text; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Windows; using System.W…

redux logic_Redux-Logic简介
redux logicby Sam Ollason通过萨姆奥拉森(Sam Ollason) Redux-Logic简介 (An Introduction to Redux-Logic) This article will go through a high-level overview of Redux-Logic. We will look at what is it, why it is needed, how it differs from other Redux middlewa…

[SDOI2009]HH的项链
题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链。HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义。HH 不断地收集新的贝壳,因此,他的项链变得越来越长。有一…
DvaJS 入门, 快速上手Dva
为什么要使用Dva? React 没有解决的问题 React 本身只是一个 DOM 的抽象层,使用组件构建虚拟 DOM。 如果开发大应用,还需要解决一个问题。 通信:组件之间如何通信?数据流:数据如何和视图串联起来?路由…

Static、DynamicResource学习笔记一
定义了资源后,我们可以在元素中直接使用该资源,但又分为StaticResource及DynamicResource两种方式。 StaticResource 静态资源在首次创建窗口时一次性的设置完毕,之后源资源对象本身发生的任何变化都会影响到使用该资源的元素,如果…

javascript迭代器_JavaScript迭代器概述
javascript迭代器by Joanna Gaudyn乔安娜高登(Joanna Gaudyn) JavaScript迭代器概述 (An overview of JavaScript iterators) for,for…in和for…of循环之间的区别 (The difference between for, for…in and for…of loops) Iteration might be one of the most c…

knockout学习笔记目录
关于knockout学习系列的文章已经写完,这里主要是做个总结,并且将目录罗列出来,方便查看。欢迎各位大神拍砖和讨论。 总结 kncokout是一个轻量级的UI类库,通过MVVM模式使前端的UI简单话,减少javascript代码的编写。通常…

ant Design Pro 登录状态管理
未登录自动跳转到登录页面,登录成功不跳转回登录页面的实现代码调用流程。 ant Design Pro 是一个企业中后台管理框架,开始做他,第一个肯定是要做登录,下面来看一下它是怎么登录的。 先看路由配置 Authorized.jsx代码࿱…