您的位置:首页 > Web前端 > JQuery

删除提示确认是否删除javascript/js的写法以及封装为jQuery插件

2016-11-06 20:52 711 查看
因为考虑到删除时每个页面都有的,所以把一个js’写在common。js中,并在common。jsp中进行调用和调用jq。js,这样直接导入到jsp,当页面加载的时候,自然也就为删除添加了js事件

var common = {
myconfig:function(message){
return window.confirm(message);
}
};
$().ready(function(){
/**
* 当页面进行加载的时候,给名称删除的超级连接添加一个事件
*/
$("a").each(function(){
if($(this).text()=="删除"){
$(this).unbind("click");
$(this).bind("click",function(){
return common.myconfig("您确认要删除吗?");
});
}
});
});


<%@ taglib prefix="s" uri="/struts-tags" %>
<script language="javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.2.js"></script>
<script language="javascript" src="${pageContext.request.contextPath}/js/common.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/blue/pageCommon.css" type="text/css">


<%@ include file="/WEB-INF/jsp/common/common.jsp"%>

<s:a action="">删除</s:a>


将js代码封装成jquery插件

插件jquery-confirm.js
(function(jQuery){
$.confirm = function(confirJson){
$("a").each(function(){
if($(this).text()=="删除"){
$(this).unbind("click");
$(this).bind("click",function(){
confirJson.callback();
return window.confirm(confirJson.message);
});
}
});
}
})(jQuery);
<!-- 通过参数获取页面指定标签,给标签添加click事件 -->
(function (jQuery){
$.confirm = function (Json){
}

})(jQuery)


common.js //
$().ready(function(){
/**
* 当页面进行加载的时候,给名称删除的超级连接添加一个事件
*/
$.confirm({
message:"您确认是否要删除吗?",
callback:function(){
alert("我就是牛");
}
});
});


<%@ taglib prefix="s" uri="/struts-tags" %>
<script language="javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.2.js"></script>
<script language="javascript" src="${pageContext.request.contextPath}/js/jquery-confirm.js"></script>
<script language="javascript" src="${pageContext.request.contextPath}/js/common.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/blue/pageCommon.css" type="text/css">
通过common.js传入参数到插件,最后将两个js包含在common.jsp中,再把common.jsp引入到jsp.只要遇到删除。就自动触发这个事件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐