当我在两个元素之间放置一个“=”而不是两个或三个相等的字符时,程序就会出错并且无法正常工作 我想知道为什么我不能使两个不同类型的元素通过一个等号(=)彼此相等,但我可以使用三个或两个元素,但它们不是同一类型 看我的代码:
const progress=document.getElementById('progress')
const prev=document.getElementById('prev')
const next=document.getElementById('next')
const circles=document.querySelectorAll('.circle')
let currentActive=1
next.addEventListener('click',()=>{
currentActive++
if(currentActive>circles.length){
currentActive=circles.length
}
update()
})
prev.addEventListener('click',()=>{
currentActive--
if(currentActive<1){
currentActive=1
}
update ()
})
function update(){
circles.forEach((circle,idx)=> {
if(idx<currentActive){
circle.classList.add('active')
}else{
circle.classList.remove('active')
}
})
const actives=document.querySelectorAll('.active')
progress.style.width=(actives.length - 1) / (circles.length - 1) * 100 + '%'
if(currentActive=== 1){
prev.disabled=true
}else if(currentActive === circles.length){
next.disabled=true
}else{
prev.disabled=false
next.disabled=false
}
}
current active 是一个包含数字和circles的变量。length是一个包含数字的元素。 虽然它们不是同一类型,但当我输入三个等于时,程序可以正确运行,但当我输入一个等于时,程序就不能正确运行。 它应该是相反的,因为它们不能是同一类型。
= 是一个赋值运算符,它将右手值分配给左手变量,另一边 == 或 === 是比较运算符,用于检查 LHS 是否等于 RHS。因此,如果您要比较值,请使用 == 或 ===。
注意: == -> 它只是检查值。 === -> 它检查对象类型和值