以删除为例,首先新建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 确定、取消功能的更多相关文章

  1. 利用bootstrap的modal组件自定义alert,confirm和modal对话框

    由于浏览器提供的alert和confirm框体验不好,而且浏览器没有提供一个标准的以对话框的形式显示自定义HTML的弹框函数,所以很多项目都会自定义对话框组件.本篇文章介绍自己在项目中基于bootst ...

  2. JavaScript实现自定义alert弹框

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAh0AAAFkCAYAAACEpYlzAAAfj0lEQVR4nO3dC5BddZ0n8F93pxOQCO

  3. 使用Typescript重构axios(十八)——请求取消功能:总体思路

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  4. 使用Typescript重构axios(十九)——请求取消功能:实现第二种使用方式

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  5. 使用Typescript重构axios(二十)——请求取消功能:实现第一种使用方式

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  6. 使用Typescript重构axios(二十一)——请求取消功能:添加axios.isCancel接口

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  7. 使用Typescript重构axios(二十二)——请求取消功能:收尾

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  8. JQuery实现点击关注和取消功能

    点赞,网络用语,表示“赞同”.“喜爱”. 该网络语来源于网络社区的“赞”功能.送出和收获的赞的多少.赞的给予偏好等,在某种程度能反映出你是怎样的人以及处于何种状态.点赞的背后,反映出你自己.与之对应的 ...

  9. vue的表单编辑删除,保存取消功能

    过年回来第一篇博客,可能说的不是很清楚,而且心情可能也不是特别的high,虽然今天是元宵,我还在办公室11.30在加班,但就是想把写过的代码记下来,怕以后可能真的忘了.(心将塞未塞,欲塞未满) VUE ...

  10. Python进阶:自定义对象实现切片功能

    2018-12-31 更新声明:切片系列文章本是分三篇写成,现已合并成一篇.合并后,修正了一些严重的错误(如自定义序列切片的部分),还对行文结构与章节衔接做了大量改动.原系列的单篇就不删除了,毕竟也是 ...

随机推荐

  1. 关于_getattr_方法的一些理解

    在学习rest framework的过程中,rest framework的request是经过重构的,但是如果调用重构对象request中的属性,如果属性不存在会调用原request对象中的属性,它使 ...

  2. Linux part2(系统的相关设置变更)

    修改Linux的系统语言 首先查看当前系统的语言 1.echo $LANG 查看当前操作系统的语言 中文:zh_CN.UTF-8 英文: en_US.UTF-8 2.临时更改默认语言,当前立即生效 重 ...

  3. go声明和初始化

    go声明和初始化 当我们第一次看见变量和声明时,我们仅仅看见一些内置的类型,比如整型和字符串.现在我们将学习结构体,并且我们会深入学习包括指针的内容. 通过一种最简单的方式去创建一个结构体值类型: g ...

  4. How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04

    How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04 链接来自于:https://www.digitalo ...

  5. 【Flutter学习】基本组件之基本按钮组件

    一,概述 由于Flutter是跨平台的,所以有适用于Android和iOS的两种风格的组件.一套是Google极力推崇的Material,一套是iOS的Cupertino风格的组件.无论哪种风格,都是 ...

  6. NOIp2018集训test-10-22 (联考六day2)

    中间值 两个log肯定会被卡.我用的第一种做法,就是要各种特判要在两个序列都要二分比较麻烦. //Achen #include<bits/stdc++.h> #define For(i,a ...

  7. redis集群创建时报错:Sorry, can't connect to node

    1.redis集群创建时报错:Sorry, can't connect to node ip,端口等都配置正确的话,还需要将redis.conf文件中的密码注释掉    # requirepass 1 ...

  8. 关于Web中的图标使用问题

    挺懒的,这里做个记录. Web中的图标使用问题 : https://github.com/amfe/article/issues/2

  9. 矢量切片应用中geoserver与geowebcache分布式部署方案

    在进行GIS项目开发中,常使用Geoserver作为开源的地图服务器,Geoserver是一个JavaEE项目,常通过Tomcat进行部署.而GeoWebCache是一个采用Java实现用于缓存WMS ...

  10. PDO如何完成事务操作

    起因 无意间翻看极客学院的APP,准备找一些教程看看.看到一篇PDO 安全处理与事务处理,一想对MySQL的事务处理仅仅停留在概念上(知道执行多条语句,其中一个失败了,就会回滚操作).但是把概念变成代 ...