87,917
社区成员
发帖
与我相关
我的任务
分享
$(document).bind("click",{“数据1”:“数据值1”},closeDiv)
function closeDiv(e){
$_clicked=$(e.target)
if(!$_clicked.hasClass(弹出层div的class或者其他什么class))
弹出层close
alert(e.target.data.数据1)
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript" type="text/javascript">
// 工具类
var EventUtil = {
/**
* 添加事件
* @param oTarget: DOM对象
* @param sEventType:事件类型
* @param fn:函数名
*/
addEvent : function(oTarget,sEventType,fn){
if(oTarget.addEventListener){
this.addEvent = function(oTarget,sEventType,fn) {
oTarget.addEventListener(sEventType,fn,false);
}
}else if(oTarget.attachEvent){
this.addEvent = function(oTarget,sEventType,fn) {
oTarget.attachEvent("on" + sEventType,fn);
}
}else{
this.addEvent = function(oTarget,sEventType,fn) {
oTarget["on" + sEventType] = fn;
}
}
// 调用新函数
this.addEvent(oTarget,sEventType,fn);
}
/**
* 移除事件
* @param oTarget: DOM对象
* @param sEventType:事件类型
* @param fn:函数名,如果DOM对象上有多个click事件的话,可以指定移除哪一个函数
*/
,removeEvent : function(oTarget,sEventType,fn){
if(oTarget.removeEventListener){
this.removeEvent = function(oTarget,sEventType,fn) {
oTarget.removeEventListener(sEventType,fn,false);
}
}else if(oTarget.detachEvent){
this.removeEvent = function(oTarget,sEventType,fn) {
oTarget.detachEvent("on"+sEventType,fn);
}
}else {
this.removeEvent = function(oTarget,sEventType,fn) {
oTarget["on"+sEventType] = null;
}
}
this.removeEvent(oTarget,sEventType,fn);
}
/**
* 格式化事件对象,做到IE与DOM的统一
* @param oEvent:事件对象
*/
,formatEvent : function(oEvent){
if(!+[1,]){ // IE
oEvent.charCode = (oEvent.type == "keypress")?oEvent.charCode:0;
oEvent.eventPhase = 2;
oEvent.isChar = (oEvent.charCode > 0);
oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
oEvent.pageY = oEvent.clientY + document.body.scrollTop;
// 阻止某个事件的默认行为
oEvent.preventDefault = function(){
this.returnValue = false;
}
if(oEvent.type == "mouseout"){
oEvent.relateTarget = oEvent.toElement;
} else if(oEvent.type == "mouseover"){
oEvent.relateTarget = oEvent.fromElement;
}
// 阻止冒泡
oEvent.stopPropagation = function(){
this.cancelBubble = true;
}
oEvent.target = oEvent.srcElement;
oEvent.timestamp = (new Date()).getTime();
}
return oEvent;
}
/**
* 格式化事件对象
*/
,getEvent : function(){
if(window.event){
return this.formatEvent(window.event);
}else {
return this.getEvent.caller.arguments[0];
}
}
};
function ff(str,num) {
var e = EventUtil.getEvent();
alert(str + num)
// 阻止冒泡,可以把这句注释掉看效果
e.stopPropagation();
}
</script>
</head>
<body>
<div onclick="alert(1)">
<button id="btnShow" onclick="ff('abc',123);">button</button>
</div>
</body>
</html>
这个工具类,参考下
var str="tetst";
jQuery('body').on('click', function(){
console.log(str);
})
只能通过这种方式取其它的值<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function x(e,str){
var a=e||window.event;
if(a.stopPropagation){
a.stopPropagation();
}else{
a.cancelBubble=true;
}
alert(str);
}
window.onload=function(){
document.getElementById('createtable').onclick=function(){
alert('a');
}
document.getElementById('span').onclick=function(e){
x(e,'mm');
}
}
</script>
</head>
<body>
<div id="createtable">12<span id="span">34</span></div>
</body>
</html>