如何在视图中创建比决定它的逻辑更高的效果?

发布于 2024-09-15 20:01:05 字数 333 浏览 1 评论 0原文

我的视图中有一个简单的 if 语句,它返回 x = 0 或 1。基于这个简单的结果,我想更改包含整个部分的 div 的样式。

<div>
  conditional that returns x=1 vs x=0 (and a few displayed items)
  based on this loop, restyle the div
</div>

假设,如果 x = 1,我想要设置背景颜色:rgb(210,215,220);

我怎样才能做到这一点?我对 Javascript 没有经验,但我确信所需的任何代码都会非常简单。谢谢你!

I have a simple if statement in my view that returns x = 0 or 1. Based on this simple result, I want to change the styling of the div that contains the entire section.

<div>
  conditional that returns x=1 vs x=0 (and a few displayed items)
  based on this loop, restyle the div
</div>

Let's say, if x = 1, I want to make background-color:rgb(210,215,220);

How can I accomplish this? I am not experienced with Javascript but I'm sure any code required would be very simple. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蘸点软妹酱 2024-09-22 20:01:05

将 ID 添加到您的 DIV,如下所示:

<div ID="colorThis">
  conditional that returns x=1 vs x=0 (and a few displayed items)
  based on this loop, restyle the div
</div>

然后执行以下操作:

if(x==1)
{
  ​document.getElement​ById('colorThis').style.background = 'rgb(210,215,220)'​​​​;​​​​​​​​​​​​​​
}

Add an ID to your DIV like:

<div ID="colorThis">
  conditional that returns x=1 vs x=0 (and a few displayed items)
  based on this loop, restyle the div
</div>

Then do this:

if(x==1)
{
  ​document.getElement​ById('colorThis').style.background = 'rgb(210,215,220)'​​​​;​​​​​​​​​​​​​​
}
二智少女 2024-09-22 20:01:05
<script>
if(x==1)
{
$("#divid").css("background-color","rgb(210,215,220)");
}
</script>

包含 jQuery,并将 div 的 id 赋予 div

<script>
if(x==1)
{
$("#divid").css("background-color","rgb(210,215,220)");
}
</script>

Include jQuery, and give the div's id to divid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文