以删除为例,首先新建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. js 实现 间隙滚动效果

    代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  2. Vue学习笔记【3】——Vue指令之v-bind的三种用法

    直接使用指令v-bind 使用简化指令: 在绑定的时候,拼接绑定内容::title="btnTitle + ', 这是追加的内容'" <!DOCTYPE html> & ...

  3. 在SpringCloud中MAVEN配置文件中的更改

    <mirrors> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> ...

  4. SCP-bzoj-1054

    项目编号:bzoj-1054 项目等级:Safe 项目描述: 戳这里 特殊收容措施: 直接状压BFS即可,我实现的比较渣..复杂度O(45*216). 附录: #include <bits/st ...

  5. (转)NAT与NAT穿越学习总结--ICE过程讲的不错

    转:http://cgs1999.iteye.com/blog/1994072 1.引言网络地址转换(Network Address Translation,简称NAT)是一种在IP分组通过路由器或防 ...

  6. sklearn中standardscaler中fit_transform()和transform()有什么区别,应该怎么使用?

    在根据机器学习书中提供的实例中,看到需要对训练和测试的特征数据进行标准化. 但是使用的是有两个函数, 对于训练数据,使用的是fit_transform()函数 对于测试数据,使用的是tansform( ...

  7. go gin

    1.安装 go get -u github.com/gin-gonic/gin 2. package main import "github.com/gin-gonic/gin" ...

  8. Openstack贡献者须知 — OpenPGP/SSH/CLA贡献者协议

    目录 目录 前言 Openstack基金委员会 Openstack贡献者须知 注册Openstack In Launchpad 生成并上传OpenPGP密钥 生成并上传SSH公钥 Join The O ...

  9. RabbitMQ使用(二)

    1.消息确认消费 1. 生产者端发消息时,加参数 properties=pika.BasicProperties( delivery_mode=2, # make message persistent ...

  10. 在Windows上安装部署Cuckoo

    1. Cuckoo使用的第三方工具及库 Yara:http://plusvic.github.io/yara/ Pydeep:https://github.com/kbandla/pydeep Yar ...