
批改狀態(tài):合格
老師批語:
事件三要素
1.事件名稱:字符串’click’、’keydown’、’scroll’
2.事件主體:元素<button>、<div>、<p>、
3.事件方法:函數(shù)function(ev){…}
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
</ul>
<script>
const items=document.querySelectorAll('li');
items.forEach(function(item){
item.onclick=function(){
console.log(event.currentTarget);
// 阻止冒泡
event.stopPropagation();
};
});
// 事件冒泡:當前元素的事件會向上(父級)傳遞,父級有同名事件會自動觸發(fā)
// 關(guān)鍵詞:父級、同名
// li的父級ul
document.querySelector('ul').onclick=()=>console.log('li的父級ul',event.currentTarget);
// ul的父級body
document.body.onclick=()=>console.log('ul的父級body',event.currentTarget);
// body的父級html
document.documentElement.onclick=()=>console.log('body的父級html',event.currentTarget);
// html的父級document
document.onclick=()=>console.log('html的父級document',event.currentTarget);
// document的父級window
window.onclick=()=>console.log('document的父級window',event.currentTarget);
</script>
<body>
<ul>
<li data-index="1">item1</li>
<li data-index="2">item2</li>
<li data-index="3">item3</li>
<li data-index="4">item4</li>
<li data-index="5">item5</li>
</ul>
<script>
// 事件代理:將子元素的事件,委托在父元素上出發(fā),以簡化代碼
// 1.先獲取所有l(wèi)i的父級ul
const ul=document.querySelector('ul');
// 2.給ul添加事件
ul.onclick=function(){
// 事件目標:li,子元素
// 為了知道當前事件的觸發(fā)者,可以為它添加自定義屬性data-...
console.log(event.target,event.target.dataset.index);
// 事件目標:ul,父元素
console.log(event.currentTarget);
}
</script>
<script>
let str='Php中文網(wǎng)';
// 1.str.charAt()
console.log(str.charAt(3));
// 2.str.indexOf()
console.log(str.indexOf('文'));
// 3.str.search()
console.log(str.search('文'));
// 4.str.concat()
console.log(str.concat('(','php.cn',')'));
// 5.str.replace()
console.log(str.replace('中文網(wǎng)','.cn'));
// 6.str.slice()
console.log(str.slice(0,3));
// 7.str.substr()
console.log(str.substr(0,3));
// 8.str.split()把字符串以分割符為界點分割成數(shù)組
console.log(str.split('h',[2]));
// 9.str.trim()去掉字符串前后空格
console.log(str.trim('網(wǎng)'));
// 10.str.charCodeAt()指定位置的字符轉(zhuǎn)ASCII碼
console.log(str.charCodeAt(1));
// 11.str.toUpperCase()轉(zhuǎn)成大寫
console.log(str.toUpperCase('php中文網(wǎng)'));
// 12.str.toLowerCase()轉(zhuǎn)為小寫
console.log(str.toLowerCase('php中文網(wǎng)'));
</script>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號