每次单击时更改元素的颜色(反之亦然)

空的

嘿,我正在尝试将元素背景颜色更改为米色时为红色,红色时为米色。我试过使用正确的 if 语句,但我似乎无法找到解决方案。如果有人帮助我,我会非常高兴!谢谢。

HTML

<div id="first" class="firstdiv">
    <h2>DenemeTahtası</h2>
    <div>
        <section id="lorem">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Debitis sit dolor, soluta ab illum aperiam
            quam! Eum, delectus aliquam ipsa perspiciatis modi perferendis laudantium aut vel, ut veniam quae
            eligendi?
        </section>
    </div>
</div>

CSS

.firstdiv{
background-color: antiquewhite;
height: 50vh;

h2{
   
    margin-left: 40%;
    padding-top: 20px;

}

#lorem{
    margin: 0 auto;
    width: 65%;
    margin-top: 25px;
    text-align: center;
}

}

JS

    let exp = document.querySelector("#lorem")
exp.style.backgroundColor = "beige"
exp.addEventListener("click", colorChange)

function colorChange() {

    if (exp.style.backgroundColor = "beige") {
        exp.style.backgroundColor = "red"
    }
    else if (exp.style.backgroundColor = "red") {
        exp.style.backgroundColor == "beige"

    }

}
罗布森

这非常接近正确。需要注意的是等号的数量。

  • 在进行比较时,您应该使用==. 您也可以使用===which 相同,但变量类型也必须匹配。
  • 做作业时你应该使用=.

这意味着这段代码:

if (exp.style.backgroundColor = "beige") {
    exp.style.backgroundColor = "red"
} else if (exp.style.backgroundColor = "red") {
    exp.style.backgroundColor == "beige"
}

需要是:

if (exp.style.backgroundColor == "beige") {
    exp.style.backgroundColor = "red"
} else if (exp.style.backgroundColor == "red") {
    exp.style.backgroundColor = "beige"
}

最终的结果将是:

let exp = document.querySelector("#lorem")
exp.style.backgroundColor = "beige"
exp.addEventListener("click", colorChange)

function colorChange() {
    if (exp.style.backgroundColor == "beige") {
        exp.style.backgroundColor = "red"
    } else if (exp.style.backgroundColor == "red") {
        exp.style.backgroundColor = "beige"
    }
}
.firstdiv {
  background-color: antiquewhite;
  height: 50vh;
}

h2 {
    margin-left: 40%;
    padding-top: 20px;
}

#lorem {
    margin: 0 auto;
    width: 65%;
    margin-top: 25px;
    text-align: center;
}
<div id="first" class="firstdiv">
    <h2>DenemeTahtası</h2>
    <div>
        <section id="lorem">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Debitis sit dolor, soluta ab illum aperiam
            quam! Eum, delectus aliquam ipsa perspiciatis modi perferendis laudantium aut vel, ut veniam quae
            eligendi?
        </section>
    </div>
</div>

米色/红色矩形本质上是一个可点击的按钮,因此您可能需要添加几个 CSS 属性以使用户更直观。具体来说:

cursor: pointer; /* turn the mouse cursor into a hand */
user-select: none; /* disable selecting of text */

这样做的结果是:

let exp = document.querySelector("#lorem")
exp.style.backgroundColor = "beige"
exp.addEventListener("click", colorChange)

function colorChange() {
    if (exp.style.backgroundColor == "beige") {
        exp.style.backgroundColor = "red"
    } else if (exp.style.backgroundColor == "red") {
        exp.style.backgroundColor = "beige"
    }
}
.firstdiv {
  background-color: antiquewhite;
  height: 50vh;
}

h2 {
    margin-left: 40%;
    padding-top: 20px;
}

#lorem {
    margin: 0 auto;
    width: 65%;
    margin-top: 25px;
    text-align: center;
    cursor: pointer;
    user-select: none;
}
<div id="first" class="firstdiv">
    <h2>DenemeTahtası</h2>
    <div>
        <section id="lorem">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Debitis sit dolor, soluta ab illum aperiam
            quam! Eum, delectus aliquam ipsa perspiciatis modi perferendis laudantium aut vel, ut veniam quae
            eligendi?
        </section>
    </div>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

单击时更改元素的颜色

单击时如何将 favicon 的值从 false 更改为 true,反之亦然?

单击 EN 更改为 AR,反之亦然

单击元素时如何更改元素的背景颜色?

想要在用户单击时像从按钮一样将按钮更改为默认状态,反之亦然

如何通过单击更改元素颜色

单击更改元素背景颜色

如何使pygame程序在单击时从绘制状态变为非绘制状态,反之亦然?

将元素从 Array1 更改为 Array2,反之亦然

D3.js-与图例交互时突出显示图表元素,反之亦然

鼠标悬停在链接上(悬停)时更改图像,反之亦然

在点击表格标题时将顺序从 asc 更改为 desc 或反之亦然

动画颜色从上到下变化,反之亦然

单击按钮做两件事,反之亦然

Javascript,使用if语句更改元素的单击颜色

jQuery取消选中,子元素选中并反之亦然

每次单击按钮时如何更改颜色?

如何将大写更改为小写,反之亦然?

更改播放按钮以在Angular中暂停,反之亦然

在UDF中将句号更改为逗号,反之亦然

更改时自动调整相关状态,反之亦然

如何根据CheckBox状态更改ListBox选择,反之亦然?

将登录文本按钮更改为注销,反之亦然

将 td 内容更改为选择,反之亦然

如何在 DataGrid 中检测是否单击了行并将其列从 true 更改为 false,反之亦然?

React-每次用户单击屏幕上的任何位置时更改元素文本的最佳方法是什么?

当使用javascript将iPad的方向从横向更改为纵向时,如何将变量设置为false,反之亦然?

闪亮:当通过actionButton评估输入条件时,如何更改输出(可视数据变为警告消息,反之亦然)

悬停CSS时更改元素的背景颜色