亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 34
粉絲 0
評論 0
訪問量 28613
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
事件傳遞機制與事件冒泡/事件代理及字符串常用的api
OC的PHP大牛之路
原創(chuàng)
482人瀏覽過

事件傳遞機制與事件冒泡/事件代理

事件三要素
1.事件名稱:字符串’click’、’keydown’、’scroll’
2.事件主體:元素<button>、<div>、<p>、
3.事件方法:函數(shù)function(ev){…}

事件傳遞機制與事件冒泡

  1. <ul>
  2. <li>item1</li>
  3. <li>item2</li>
  4. <li>item3</li>
  5. <li>item4</li>
  6. <li>item5</li>
  7. </ul>
  8. <script>
  9. const items=document.querySelectorAll('li');
  10. items.forEach(function(item){
  11. item.onclick=function(){
  12. console.log(event.currentTarget);
  13. // 阻止冒泡
  14. event.stopPropagation();
  15. };
  16. });
  17. // 事件冒泡:當前元素的事件會向上(父級)傳遞,父級有同名事件會自動觸發(fā)
  18. // 關(guān)鍵詞:父級、同名
  19. // li的父級ul
  20. document.querySelector('ul').onclick=()=>console.log('li的父級ul',event.currentTarget);
  21. // ul的父級body
  22. document.body.onclick=()=>console.log('ul的父級body',event.currentTarget);
  23. // body的父級html
  24. document.documentElement.onclick=()=>console.log('body的父級html',event.currentTarget);
  25. // html的父級document
  26. document.onclick=()=>console.log('html的父級document',event.currentTarget);
  27. // document的父級window
  28. window.onclick=()=>console.log('document的父級window',event.currentTarget);
  29. </script>

事件代理

  1. <body>
  2. <ul>
  3. <li data-index="1">item1</li>
  4. <li data-index="2">item2</li>
  5. <li data-index="3">item3</li>
  6. <li data-index="4">item4</li>
  7. <li data-index="5">item5</li>
  8. </ul>
  9. <script>
  10. // 事件代理:將子元素的事件,委托在父元素上出發(fā),以簡化代碼
  11. // 1.先獲取所有l(wèi)i的父級ul
  12. const ul=document.querySelector('ul');
  13. // 2.給ul添加事件
  14. ul.onclick=function(){
  15. // 事件目標:li,子元素
  16. // 為了知道當前事件的觸發(fā)者,可以為它添加自定義屬性data-...
  17. console.log(event.target,event.target.dataset.index);
  18. // 事件目標:ul,父元素
  19. console.log(event.currentTarget);
  20. }
  21. </script>

字符串常用的api

  1. <script>
  2. let str='Php中文網(wǎng)';
  3. // 1.str.charAt()
  4. console.log(str.charAt(3));
  5. // 2.str.indexOf()
  6. console.log(str.indexOf('文'));
  7. // 3.str.search()
  8. console.log(str.search('文'));
  9. // 4.str.concat()
  10. console.log(str.concat('(','php.cn',')'));
  11. // 5.str.replace()
  12. console.log(str.replace('中文網(wǎng)','.cn'));
  13. // 6.str.slice()
  14. console.log(str.slice(0,3));
  15. // 7.str.substr()
  16. console.log(str.substr(0,3));
  17. // 8.str.split()把字符串以分割符為界點分割成數(shù)組
  18. console.log(str.split('h',[2]));
  19. // 9.str.trim()去掉字符串前后空格
  20. console.log(str.trim('網(wǎng)'));
  21. // 10.str.charCodeAt()指定位置的字符轉(zhuǎn)ASCII碼
  22. console.log(str.charCodeAt(1));
  23. // 11.str.toUpperCase()轉(zhuǎn)成大寫
  24. console.log(str.toUpperCase('php中文網(wǎng)'));
  25. // 12.str.toLowerCase()轉(zhuǎn)為小寫
  26. console.log(str.toLowerCase('php中文網(wǎng)'));
  27. </script>
批改老師:PHPzPHPz

批改狀態(tài):合格

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費學(xué)