DOM Events API:事件委托和stopPropagation

时间:2011-11-17 06:13:18

标签: javascript jquery javascript-events

这是一个使用jQuery 1.7的代码:

<div class="test">
  <div class="bu">
    <a>
      bu here
    </a>
  </div>
</div>
<script src="http://code.jquery.com/jquery-1.7.js"></script>
<script>
$(document).on('click', '.test', function () { alert(0); return false; });
$(document).on('click', '.bu', function () { alert(1); return false; });
$(document).on('click', '.bu', function () { alert(2); return false; });
</script>

点击.test&gt; .bu将警告“1”并警告“2”,但不警告“0”

我的问题是:如何在没有jQuery的情况下(在本机DOM API上)做同样的事情?似乎,如果没有实现我自己的库,我无法使用Native DOM API ...

谢谢!

3 个答案:

答案 0 :(得分:13)

你走了:

document.addEventListener( 'click', function ( e ) {
    if ( hasClass( e.target, 'bu' ) ) {            
        // .bu clicked
        // do your thing
    } else if ( hasClass( e.target, 'test' ) ) {
        // .test clicked
        // do your other thing
    }   
}, false );

其中hasClass

function hasClass( elem, className ) {
    return elem.className.split( ' ' ).indexOf( className ) > -1;
}

现场演示: http://jsfiddle.net/Nrxp5/30/

答案 1 :(得分:3)

<div class="a">
    <div class="b">
      <div class="c" style="border: 1px solid silver; width: 80px; text-align: center;line-height: 80px;">
          click me!
      </div>
    </div>
</div>

<script>

// Element.prototype.matchesSelector
(function (x) {
  var i;
  if (!x.matchesSelector) {
    for (i in x) {
      if (/^\S+MatchesSelector$/.test(i)) {
        x.matchesSelector = x[i];
        break;
      }
    }
  }
}(Element.prototype));

Document.prototype.on =
Element.prototype.on = function (eventType, selector, handler) {
  this.addEventListener(eventType, function listener(event) {
    var t = event.target,
      type = event.type,
      x = [];
    if (event.detail && event.detail.selector === selector && event.detail.handler === handler) {
      return this.removeEventListener(type, listener, true);
    }
    while (t) {
      if (t.matchesSelector && t.matchesSelector(selector)) {
        t.addEventListener(type, handler, false);
        x.push(t);
      }
      t = t.parentNode;
    }
    setTimeout(function () {
      var i = x.length - 1;
      while (i >= 0) {
        x[i].removeEventListener(type, handler, false);
        i -= 1;
      }
    }, 0);
  }, true);
};

Document.prototype.off =
Element.prototype.off = function (eventType, selector, handler) {
  var event = document.createEvent('CustomEvent');
  event.initCustomEvent(eventType, false, false, {selector: selector, handler: handler});
  this.dispatchEvent(event);
};

document.on('click', '.b', function () {
  alert(2);
});
document.on('click', '.a', function () {
  alert(1);
});
document.on('click', '.b', function (event) {
  alert(3);
  event.stopPropagation();
});

</script>

答案 2 :(得分:0)

这是基于原型的版本

Document.prototype.on = function(event, target = null, callBack){
    this.addEventListener(event, function(event){
        let len = target.length, i = 0;
        while(i < len){
            if(event.target === target[i]){
                callBack.call(target[i], event);
            }
            i ++;
        }
    }, false);
};

用法就像jQuery一样:

let btns = document.getElementsByTagName('button');
document.on('click', btns, function(event){
    console.log(this.innerText)
});

下面是一个实时示例:

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body bgcolor="#000">
<button>hello1</button>
<button>hello2</button>
<script type="text/javascript">
	Document.prototype.on = function(event, target = null, callBack = function(){}){
		this.addEventListener(event, function(event){
			let len = target.length, i = 0;
			while(i < len){
				if(event.target === target[i]){
					callBack.call(target[i], event);
				}
				i ++;
			}
		}, false);
	};


//example of usage
	let btns = document.getElementsByTagName('button');
	document.on('click', btns, function(t){
		alert(this.innerText);
	})

//add elements after delegating an event
	let newBtn = document.createElement('button');
		newBtn.innerText = 'btnNew';
		document.body.appendChild(newBtn);
    
//delay to create
setTimeout(function(){
let newBtn = document.createElement('button');
		newBtn.innerText = 'another btnNew';
		document.body.appendChild(newBtn);
},2000);
</script>
</body>
</html>