<div id="div"> <a class="aa" id="btn1" class="btn" href="www.baidu.com">1</a> <a class="aa" id="btn2" class="btn" href="www.baidu.com">2</a> <a class="aa" id="btn3" class="btn" href="www.baidu.com">3</a> <a class="aa" id="btn4" class="btn" href="www.baidu.com">4</a> </div> var div = document.getElementById('div') function bindEvent(elem,type,selector,fn){ if(fn == null){ fn = selector; selector = null; } elem.addEventListener(type,function(e){ var target = e.target; e.preventDefault() if(selector = null){ fn(e) }else{ if(target.matches(selector)){ fn.call(target,e) } } }) } bindevent(div, 'click', 'a', function(e) { alert(this.innerHTML) }) //部分代码修改后 elem.addEventListener(type, function(e) { var target = e.target; e.preventDefault() if(selector) { if(target.matches(selector)) { fn.call(target, e) } } else { fn(e) } })
如上代码,之前没有效果,后来只是将判断的顺序变了下,就好了,这是什么原因?求大神指点
慕spring
qq_莫非
相关分类