10 examples of 'jquery add event listener' in JavaScript

Every line of 'jquery add event listener' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
188function addListener(element, type, callback) {
189 if (element.addEventListener) element.addEventListener(type, callback);
190 else if (element.attachEvent) element.attachEvent('on' + type, callback);
191}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

16function attachEventListener (element, event, callback) {
17 element.addEventListener(event, callback);
18}
66function addEvent(el, type, handler) {
67 jQuery(el).bind(type, handler);
68}
11function listen(evnt, elem, func) {
12 if (elem.addEventListener) { // W3C DOM
13 elem.addEventListener(evnt,func,false);
14 } else if (elem.attachEvent) { // IE DOM
15 var r = elem.attachEvent("on"+evnt, func);
16 return r;
17 }
18};
70function attachListener(element, event, fn){
71 element.addEventListener(getEventName(event), fn);
72}
126export function on(element, type, listener) {
127 if (type === event.INPUT) {
128 addInputListener(element, listener)
129 }
130 else if (type === event.CHANGE && isBox(element)) {
131 addChangeListener(element, listener)
132 }
133 else {
134 element.attachEvent(`on${type}`, listener)
135 }
136}
10function addEvent(dom, type, fn) {
11 //对于支持DOM2级事件处理程序addEventListener 方法的浏览器
12 if (dom.addEventListener) {
13 dom.addEventListener(type, fn, false)
14 } else if (dom.attachEvent) {//对于不支持addEventListener 方法但支持attachEvent 方法的浏览器
15 dom.attachEvent('on' + type, fn);
16 } else {
17 dom['on' + type] = fn;
18 }
19}
98function _addEvent( evnt, elem, func ){
99
100 try{
101
102 if( elem.addEventListener ){
103
104 elem.addEventListener( evnt, func, false );
105
106 }else if( elem.attachEvent ){
107
108 var r = elem.attachEvent( "on" + evnt, func );
109
110 }
111
112 return true;
113
114 }catch( e ){
115
116 return false;
117
118 }
119
120};
481function listen(e, el, f) {
482 if (el.addEventListener) el.addEventListener(e,f,false);
483 else if (el.attachEvent) return el.attachEvent('on'+e,f);
484 else alert('Cannot listen events.');
485}
9function addEventListener (element, event, funcs, opt) {
10
11 function fn (e) {
12 let data;
13 let target = e.target || e.srcElement;
14 e = e || window.e;
15
16 if (e.type === SCROLL) {
17 if (target === document) {
18 data = { scrollTop: document.body.scrollTop, scrollLeft: document.body.scrollLeft }
19 } else {
20 data = { scrollTop: target.scrollTop, scrollLeft: target.scrollLeft }
21 }
22 }
23
24 funcs.forEach(function (f) {
25 f(e, data);
26 })
27 }
28
29 if (_.isObject(opt)) {
30 if (_.isInteger(opt.throttle) && _.isFinite(opt.throttle) && opt.throttle > -1) {
31 console.log('Set throttle as ' + opt.throttle);
32 fn = _.throttle(fn, opt.throttle);
33 }
34
35 if (_.isInteger(opt.debounce) && _.isFinite(opt.debounce) && opt.debounce > -1) {
36 console.log('Set debounce as ' + opt.debounce);
37 fn = _.debounce(fn, opt.debounce);
38 }
39 }
40
41 // https://github.com/wangpin34/vue-scroll/issues/1
42 if (event === SCROLL) {
43 if(element === document.body || element === document || element === window) {
44 document.onscroll = fn;
45 } else {
46 if (element.addEventListener) {
47 element.addEventListener(event, fn);
48 } else {
49 element.attachEvent('on' + event, fn);
50 }
51 }
52 }
53
54}

Related snippets