自定义alert 确定、取消功能
以删除为例,首先新建html
<table border="1" cellpadding="0" cellspacing="0" id="myTab">
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td>王二狗</td>
<td>24</td>
<td>男</td>
<td><a href="#" class="delete">删除</a></td>
</tr>
<tr>
<td>王小何</td>
<td>26</td>
<td>女</td>
<td><a href="#" class="delete">删除</a></td>
</tr>
</tbody>
</table>
引入<script src="js/jquery.js"></script>
引入<script src="js/mybase.js"></script>,下面是mybase.js的代码:
/*
* @Author: Administrator
* @Date: 2017-01-11 15:12:25
* @Last Modified by: Administrator
* @Last Modified time: 2017-01-11 15:13:33
*/ 'use strict';
var $window = $(window);
var $body = $('body'); function pop(arg){
if(!arg) {
console.error('pop title is required');
} var conf = {}, $box, $mask, $title, $content, $confirm, $cancel, timer, dfd, confirmed; dfd = $.Deferred(); if(typeof arg == 'string')
conf.title = arg;
else{
conf = $.extend(conf,arg);
} $box = $('<div>' +
'<div class="pop_title">'+ conf.title +'</div>' +
'<div class="pop_content">' +
'<div>' +
'<button style="margin-right: 5px;" class="primary confirm">确定</button>' +
'<button class="cancel">取消</button></div>' +
'</div>' +
'</div>')
.css({
color: '#333',
width: 300,
height: 200,
background: '#fff',
position: 'fixed',
'box-radius': 3,
'box-shadow': '0 1px 2px rgba(0,0,0,.3)'
}) $title = $box.find('.pop_title').css({
padding: '5px 10px',
'font-weight': 700,
'font-size': 20,
'text-align': 'center'
}) $content = $box.find('.pop_content').css({
padding: '5px 10px',
'text-align': 'center'
}) $confirm = $content.find('button.confirm');
$cancel = $content.find('button.cancel'); $mask = $('<div></div>')
.css({
position: 'fixed',
background: 'rgba(0,0,0,0.5)',
top: 0,
bottom: 0,
left: 0,
right: 0
}) timer = setInterval(function(){
if (confirmed !== undefined) {
dfd.resolve(confirmed);
clearInterval(timer);
dismiss_pop();
}
},50); $confirm.on('click', on_confirm); // $cancel.on('click', function(){
// confirmed = false;
// }); $cancel.on('click',on_cancel);
$mask.on('click', on_cancel); function on_confirm(){
confirmed = true;
}; function on_cancel(){
confirmed = false;
}; function dismiss_pop(){
$mask.remove();
$box.remove();
} function adjust_box_posititon() {
var window_width = $window.width(),
window_height = $window.height(),
box_width = $box.width(),
box_height = $box.height(),
move_x,
move_y;
move_x = (window_width-box_width)/2;
move_y = ((window_height-box_height)/2)-30; $box.css({
left:move_x,
top:move_y,
});
}
//resize事件作用是,当缩放窗口的时候调用adjust_box_posititon(),使$box一直居中
//但是不能一打开窗口的时候就要求缩放,所以要增加一次触发$window.resize()
$window.on('resize', function() {
adjust_box_posititon();
}); $mask.appendTo($body);
$box.appendTo($body);
//增加一次触发使$box居中
$window.resize();
return dfd.promise();
}
直接调用方法:
<script>
var $task_delete_trigger = $('.delete');
/*查找并监听所有删除按钮的点击事件*/
function listen_task_delete() {
$task_delete_trigger.on('click', function () {
var $this = $(this);
/*找到删除按钮所在的task元素*/
var $item = $this.parent().parent();
/*确认删除?*/
pop('确定删除?')
.then(function (r) {
if (r == true) {
$item.remove()
}
else{
return false
}; })
})
}
listen_task_delete();
</script>
自定义alert 确定、取消功能的更多相关文章
- 利用bootstrap的modal组件自定义alert,confirm和modal对话框
		由于浏览器提供的alert和confirm框体验不好,而且浏览器没有提供一个标准的以对话框的形式显示自定义HTML的弹框函数,所以很多项目都会自定义对话框组件.本篇文章介绍自己在项目中基于bootst ... 
- JavaScript实现自定义alert弹框
		aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAh0AAAFkCAYAAACEpYlzAAAfj0lEQVR4nO3dC5BddZ0n8F93pxOQCO 
- 使用Typescript重构axios(十八)——请求取消功能:总体思路
		0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ... 
- 使用Typescript重构axios(十九)——请求取消功能:实现第二种使用方式
		0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ... 
- 使用Typescript重构axios(二十)——请求取消功能:实现第一种使用方式
		0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ... 
- 使用Typescript重构axios(二十一)——请求取消功能:添加axios.isCancel接口
		0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ... 
- 使用Typescript重构axios(二十二)——请求取消功能:收尾
		0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ... 
- JQuery实现点击关注和取消功能
		点赞,网络用语,表示“赞同”.“喜爱”. 该网络语来源于网络社区的“赞”功能.送出和收获的赞的多少.赞的给予偏好等,在某种程度能反映出你是怎样的人以及处于何种状态.点赞的背后,反映出你自己.与之对应的 ... 
- vue的表单编辑删除,保存取消功能
		过年回来第一篇博客,可能说的不是很清楚,而且心情可能也不是特别的high,虽然今天是元宵,我还在办公室11.30在加班,但就是想把写过的代码记下来,怕以后可能真的忘了.(心将塞未塞,欲塞未满) VUE ... 
- Python进阶:自定义对象实现切片功能
		2018-12-31 更新声明:切片系列文章本是分三篇写成,现已合并成一篇.合并后,修正了一些严重的错误(如自定义序列切片的部分),还对行文结构与章节衔接做了大量改动.原系列的单篇就不删除了,毕竟也是 ... 
随机推荐
- bootstrap 好看的上传组件
			<!DOCTYPE html> <html> <head> <title></title> <link rel="style ... 
- c++之初始化列表
			#include<iostream> using namespace std; class Person{ public: int m_a; int m_b; int m_c; Perso ... 
- python  爬取拉勾网
			import requestsimport randomimport timeimport osimport csvimport pandas as pdreq_url = 'https://www. ... 
- Robot Framework:Excel操作
			robot framework 操作Excel需要安装库 ExcelLibrary pip install robotframework-ExcelLibrary 将ExcelLibrary 导入到r ... 
- NX二次开发-Block UI C++界面Enumeration(枚举)控件的获取(持续补充)
			NX9+VS2012 public: void SetBlockUIShow(); void EnumInt::SetBlockUIShow() { //获取枚举控件 PropertyList* En ... 
- NX二次开发-基于NX开发向导模板的NX对Excel读写操作(OLE方式(COM组件))
			在看这个博客前,请读者先去完整看完:NX二次开发-基于MFC界面的NX对Excel读写操作(OLE方式(COM组件))https://ufun-nxopen.blog.csdn.net/article ... 
- Nginx拓展功能合集
			一:NGINX跨域解决方式 #是否允许请求带有验证信息 add_header Access-Control-Allow-Credentials true; #允许跨域访问的域名,可以是一个域的列表,也 ... 
- Tomcat服务器配置参考
			Coyote HTTP/1.1 Connector 概述 Coyote HTTP/1.1 Connector元素是一个支持HTTP/1.1协议的Connector组件.它使Catalina除了能够执行 ... 
- ubuntu安装mysql 并对外暴露3306端口
			安装 sudo apt-get install mysql-client mysql-server vi /etc/mysql/mysql.conf.d/mysqld.cnf bind 127注掉 m ... 
- 数据挖掘 FP-tree算法C++实现及源码
			FP-growth挖掘算法 步骤一 扫描数据库,扫描数据库一次,得到频繁1-项集,把项按支持度递减排序,再一次扫描数据库,建立FP-tree 步骤二 对每个项,生成它的 条件模式库 步骤三 用条件模式 ... 
