简体   繁体   English

如何覆盖嵌套的 css class 属性?

[英]How to override nested css class attributes?

I have the following two scss files, and I'm trying to change an attribute that is set when I import an already built component.我有以下两个scss文件,我试图更改在导入已构建组件时设置的属性。 The first file is the css I'm changing, and the second is the css I'm pulling from.第一个文件是我正在更改的 css,第二个是我从中提取的 css。 I can't change the second file without messing up the component already built, but I can't seem to figure out how to modify this one attribute...我不能在不弄乱已经构建的组件的情况下更改第二个文件,但我似乎无法弄清楚如何修改这个属性......

.button-edit {
   
   .imported-button {
      width: 200px;
   }
}
.imported-button {
   width: 300px;

   .imported-button-color {
      color: red;
   }
}

I have figured out how to change the width through the first code block I've shown above, but how would I change the color from red to blue, for example?我已经想出如何通过上面显示的第一个代码块更改宽度,但是我如何将颜色从红色更改为蓝色? I've tried...我试过了...

.button-edit {
   
   .imported-button {
      width: 200px;

      .imported-button-color {
         color: blue;
      }
   }
}

... but that doesn't change anything for me. ......但这对我来说没有任何改变。 Appreciate the help!感谢帮助!

Edit: The Javascript being used is structured like...编辑:正在使用的 Javascript 的结构类似于...

<BoxButton className='button-edit'>
   <div>
      <p>Press here</p>
   </div>
</BoxButton>

where the Button component has the css attributes in the .imported-button class其中Button组件在.imported-button class 中具有 css 属性

First, you have an error in your code, why did you write className in the html code?首先,你的代码有错误,为什么在html代码中写className

<Button className='button-edit'>
   <div>
      <p>Press here</p>
   </div>
</Button>

you should just write class你应该写 class


<Button class='button-edit'>
   <div>
      <p>Press here</p>
   </div>
</Button>

Second, you have a problem arranging the elements within the scss code, as you want the blue color to activate when its parent class is deleted?其次,您在安排 scss 代码中的元素时遇到问题,因为您希望在删除其父级 class 时激活蓝色? How do you want this to be done?您希望如何完成? This command cannot be implemented because you are asking the language to break its programming to implement your goal该命令无法实现,因为您要求语言中断其编程以实现您的目标

To solve this problem, you have to remove the blue color class from inside the dev whose class you want to delete, so that it becomes as follows要解决这个问题,就得把要删除class的dev里面的蓝色class去掉,这样就变成了下面这样

.button-edit {
   
   .imported-button {
      width: 200px;
   }
  
  .imported-button-color {
    color: blue;
  }  
}

Now we come to JavaScript:现在我们来到 JavaScript:

1- We make variables that carry the father div and p 1- 我们创建携带父 div 和 p 的变量

let checkDiv  = document.querySelector(".button-edit div")

let checkDivP = document.querySelector(".button-edit div p")

2- We make a function on the button so that it contains two conditions 2- 我们在按钮上制作一个 function 以便它包含两个条件

document.querySelector(".button-edit").addEventListener("click",function(){
  
  if( checkDiv.className === "" )
    {
      checkDiv.className = "imported-button";
      
      checkDivP.className = ""
    }
  
  else
    {
      checkDiv.className = "";
      
      checkDivP.className = "imported-button-color"
    }
})
  • first condition第一个条件
if( checkDiv.className === "" )
{
  checkDiv.className = "imported-button";

  checkDivP.className = ""
}

If the class is empty, add the width class and delete the color class如果class为空,则添加宽度class,删除颜色class

  • second condition第二个条件
else
{
  checkDiv.className = "";
      
  checkDivP.className = "imported-button-color"
}

If the width class exists, delete it and add the color class如果存在宽度class,则删除,添加颜色class

The final result / https://codepen.io/emozlove/pen/JjpjmQR最终结果/https://codepen.io/emozlove/pen/JjpjmQR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM