为什么我不能将一个相等=而不是两个==
当我将一个'='而不是两个元素之间的两个或三个相等的字符时,该程序会出错并且无法正常工作 我想知道为什么我不能以一个相等的符号(=)彼此相等的两个类型的两个元素,但是我可以使用三个或两个类型 查看我的代码:
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
}
}
当前活动是一个包含数字和圆的变量。长度是包含数字的元素。 尽管它们的类型不是相同的类型,但是当我放置三个平等时,程序可以正确运行,但是当我将其放置一个相等时,它却没有。 应该相反,因为它们不能是同一类型。
when i put one '=' instead of two or three equal character between two elements,the program goes wrong and it doesn't work properly
i'd like to know why i can't make two element of different types equal to each other by one equal sign(=) but i can do with three or two but they aren't same type
look at my code:
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 is a variable which contains number and circles.length is an element which contains number.
although they aren't same type,when i put three equals,program runs correctly but when i put one equal it doesn't.
it should be opposite because they can't be same type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
=是一个分配运算符,它将为左手变量分配右手值,在另一侧==或===是比较操作员,它检查LHS是否等于RHS。因此,如果要比较值,请使用==或===。
笔记:
== - &gt;它只是检查值。
=== - &gt;它检查对象类型和值
= is an assignment operator, it will assign right hand value to left hand variable, on the other side == or === are comparison operator which checks whether LHS is equal to RHS or not. So if you are comparing the values, use == or ===.
Note:
== -> It just checks the value.
=== -> It checks object type and value