获取a标签的href值
a.onclick = function (e) {
let href = e.target.getAttribute(“href”)
}
截取字符串并且去掉空格 trim() 去掉空格的函数 slice(开始,结束)截取字符串
if (e.target.getAttribute(“href”).trim().slice(0,4)==“http”) {
console.log(e.target.getAttribute(“href”));
console.log(123);
}
slice()函数的结束那个值不包括在内,如果要截取4位的话就要往后多加1
事件绑定
let dianji = document.getElementsByTagName(‘body’)[0]
dianji.onclick = function (e) {
if (e.target.getAttribute(“href”) == ‘javascript:void(0)’) {
alert(‘我被点了’)
} else {
bg.style.display = “block”
img.style.display = “block”
}
}
事件委托
let ul = document.getElementById(‘ul’)
ul.onclick = function (e) {
e.target.style.color=“red”
}
for of不能循环对象,不然会报错
let a1=[a,b,c,d,e]
for (let b of a1) {
console.log(b) //a b c d e
}
打印出来的是值,而且不能循环对象
报错:obj is not iterable