事件

2017-08-15  本文已影响0人  Jeff12138

1、DOM0 事件和DOM2级在事件监听使用方式上有什么区别?


2、attachEvent与addEventListener的区别?


3、解释IE事件冒泡和DOM2事件传播机制?


4、如何阻止事件冒泡? 如何阻止默认事件?


5、有如下代码,要求当点击每一个元素li时控制台展示该元素的文本内容。不考虑兼容。

方法一(绑定元素):

<ul class="ct">
  <li>这里是</li>
  <li>饥人谷</li>
  <li>前端6班</li>
</ul>
<script>
  var liNodes = document.querySelectorAll('.ct li');
//for循环
  for(var i = 0;i < liNodes.length;i++){
    liNodes[i].addEventListener('click',function(){
      console.log(this.innerText);
    });
  };
/*forEach循环
  liNodes.forEach(function(node){
    node.addEventListener('click',function(){
      console.log(this.innerText);
    });
  });
*/
</script>

效果链接:http://js.jirengu.com/jigemayazo/2/edit

方法二(事件代理):

<ul class="ct">
  <li>这里是</li>
  <li>饥人谷</li>
  <li>前端6班</li>
</ul>
<script>
  var container = document.querySelector('.ct');
  container.addEventListener('click',function(e){
    if(e.target.tagName.toLowerCase() === 'li'){
      console.log(e.target.innerText);
    };
  });
</script>

效果链接:http://js.jirengu.com/cihoqiwaja/2/edit


6、补全代码。

要求:

方法一(事件代理):

<ul class="ct">
  <li>这里是</li>
  <li>饥人谷</li>
  <li>任务班</li>
</ul>
<input class="ipt-add-content" placeholder="添加内容"/>
<button id="btn-add-start">开头添加</button>
<button id="btn-add-end">结尾添加</button>
<script>
  var liNodes = document.querySelectorAll('li');
  var addStartNode = document.querySelector('#btn-add-start');
  var addEndNode = document.querySelector('#btn-add-end');
  var addIpt = document.querySelector('.ipt-add-content');
  var container = document.querySelector('.ct');
  container.addEventListener('click',function(e){
    if(e.target.tagName.toLowerCase() === 'li'){
      console.log(e.target.innerText);
    };
  });
  addStartNode.addEventListener('click',function(){
    if(addIpt.value) {
      var node = document.createElement('li');
      node.innerText = addIpt.value;
      container.insertBefore(node,container.firstChild);
    };
  });
  addEndNode.addEventListener('click',function(){
    if(addIpt.value) {
      var node = document.createElement('li');
      node.innerText = addIpt.value;
      container.appendChild(node);
    };
  });
</script>

效果链接:http://js.jirengu.com/yewazequgu/2/edit

方法二(绑定元素):

<ul class="ct">
  <li>这里是</li>
  <li>饥人谷</li>
  <li>任务班</li>
</ul>
<input class="ipt-add-content" placeholder="添加内容"/>
<button id="btn-add-start">开头添加</button>
<button id="btn-add-end">结尾添加</button>
<script>
  var liNodes = document.querySelectorAll('li');
  var addStartNode = document.querySelector('#btn-add-start');
  var addEndNode = document.querySelector('#btn-add-end');
  var addIpt = document.querySelector('.ipt-add-content');
  var container = document.querySelector('.ct');
  liNodes.forEach(function(node){
    node.addEventListener('click',function(){
      console.log(this.innerText);
    });
  });
  addStartNode.addEventListener('click',function(){
    if(addIpt.value) {
      var node = document.createElement('li');
      node.innerText = addIpt.value;
      node.addEventListener('click',function(){
        console.log(this.innerText);
      });
      container.insertBefore(node,container.firstChild);
    };
  });
  addEndNode.addEventListener('click',function(){
    if(addIpt.value) {
      var node = document.createElement('li');
      node.innerText = addIpt.value;
      node.addEventListener('click',function(){
        console.log(this.innerText);
      });
      container.appendChild(node);
    };
  });
</script>

效果链接:http://js.jirengu.com/lobemovawi/2/edit


7、补全代码。

要求:当鼠标放置在li元素上,会在img-preview里展示当前li元素的data-img对应的图片。

方法一(绑定元素):

<ul class="ct">
  <li data-img="http://cdn.jirengu.com/book.jirengu.com/img/11.jpg">鼠标放置查看图片1</li>
  <li data-img="http://cdn.jirengu.com/book.jirengu.com/img/13.jpg">鼠标放置查看图片2</li>
  <li data-img="http://cdn.jirengu.com/book.jirengu.com/img/14.jpg">鼠标放置查看图片3</li>
</ul>
<div class="img-preview"></div>
<script>
  var liNodes = document.querySelectorAll('li');
  var imgPreview = document.querySelector('.img-preview');
//for循环
  for(var i = 0;i < liNodes.length;i++){
    liNodes[i].addEventListener('mouseenter',function(){
      var dataImg = this.getAttribute('data-img');
      imgPreview.innerHTML = '![](' + dataImg + ')';
    });
  };
/*forEach循环
  liNodes.forEach(function(node){
    node.addEventListener('mouseenter',function(){
      var dataImg = this.getAttribute('data-img');
      imgPreview.innerHTML = '![](' + dataImg + ')';
    });
  });
*/
</script>

效果链接:http://js.jirengu.com/kotufonexe/2/edit

方法二(事件代理):

<ul class="ct">
  <li data-img="http://cdn.jirengu.com/book.jirengu.com/img/11.jpg">鼠标放置查看图片1</li>
  <li data-img="http://cdn.jirengu.com/book.jirengu.com/img/13.jpg">鼠标放置查看图片2</li>
  <li data-img="http://cdn.jirengu.com/book.jirengu.com/img/14.jpg">鼠标放置查看图片3</li>
</ul>
<div class="img-preview"></div>
<script>
  var container = document.querySelector('.ct');
  var liNodes = document.querySelectorAll('li');
  var imgPreview = document.querySelector('.img-preview');
  container.addEventListener('mouseover',function(e){
    var dataImg = e.target.getAttribute('data-img');
    imgPreview.innerHTML = '![](' + dataImg + ')';
  });
</script>

效果链接:http://js.jirengu.com/zopacesugo/2/edit

上一篇下一篇

猜你喜欢

热点阅读