js 有没有办法 判断一个dom元素是否已经绑定了某个事件
1个回答
2017-03-24 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
js 有办法 判断一个dom元素是否已经绑定了某个事件!
代码如下:
/*
* 事件注册
* @param Element ele
* @param String eventType
* @param Function fn
* @param Boolean isRepeat
* @param Boolean isCaptureCatch
* @return undefined
*/
function loginEvent(ele , eventType , fn , isRepeat , isCaptureCatch){
if (ele == undefined || eventType === undefined || fn === undefined) {
throw new Error('传入的参数错误!');
}
if (typeof ele !== 'object') {
throw new TypeError('不是对象!');
}
if (typeof eventType !== 'string') {
throw new TypeError('事件类型错误!');
}
if (typeof fn !== 'function') {
throw new TypeError('fn 不是函数!');
}
if (isCaptureCatch === undefined || typeof isCaptureCatch !== 'boolean') {
isCaptureCatch = false;
}
if (isRepeat === undefined || typeof isRepeat !== 'boolean') {
isRepeat = true;
}
if (ele.eventList === undefined) {
ele.eventList = {};
}
if (isRepeat === false) {
for (var key in ele.eventList)
{
if (key === eventType) {
return '该事件已经绑定过!';
}
}
}
// 添加事件监听
if (ele.addEventListener) {
ele.addEventListener(eventType , fn , isCaptureCatch);
} else if (ele.attachEvent) {
ele.attachEvent('on' + eventType , fn);
} else {
return false;
}
ele.eventList[eventType] = true;
}
代码如下:
/*
* 事件注册
* @param Element ele
* @param String eventType
* @param Function fn
* @param Boolean isRepeat
* @param Boolean isCaptureCatch
* @return undefined
*/
function loginEvent(ele , eventType , fn , isRepeat , isCaptureCatch){
if (ele == undefined || eventType === undefined || fn === undefined) {
throw new Error('传入的参数错误!');
}
if (typeof ele !== 'object') {
throw new TypeError('不是对象!');
}
if (typeof eventType !== 'string') {
throw new TypeError('事件类型错误!');
}
if (typeof fn !== 'function') {
throw new TypeError('fn 不是函数!');
}
if (isCaptureCatch === undefined || typeof isCaptureCatch !== 'boolean') {
isCaptureCatch = false;
}
if (isRepeat === undefined || typeof isRepeat !== 'boolean') {
isRepeat = true;
}
if (ele.eventList === undefined) {
ele.eventList = {};
}
if (isRepeat === false) {
for (var key in ele.eventList)
{
if (key === eventType) {
return '该事件已经绑定过!';
}
}
}
// 添加事件监听
if (ele.addEventListener) {
ele.addEventListener(eventType , fn , isCaptureCatch);
} else if (ele.attachEvent) {
ele.attachEvent('on' + eventType , fn);
} else {
return false;
}
ele.eventList[eventType] = true;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询