顺风css更改焦点按钮的颜色

qyuhtwio  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(53)

我有两个扣子,蓝色和红色。
我想设置当红色按钮被点击/聚焦蓝色按钮也应该变成红色。
如何使用Tailwind CSS

<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet"/>

<button class="focus:bg-red-700 bg-red-400 font-bold px-4 py-4 rounded-lg">
RED
</button>

<button class=" bg-blue-400 font-bold px-4 py-4 rounded-lg">
BLUE
</button>

字符串

0tdrvxhp

0tdrvxhp1#

Tailwind没有csscombinators。猜猜看,你有两种方法来实现这一点。
1.创建一个额外的css文件并添加这一行

button.bg-red-400:focus ~ button.bg-blue-400 {
  background-color: rgba(185, 28, 28, 1);
}

字符串

button.bg-red-400:focus~button.bg-blue-400 {
  background-color: rgba(185, 28, 28, 1);
}
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />

<button class="focus:bg-red-700 bg-red-400 font-bold px-4 py-4 rounded-lg">RED</button>

<button class="bg-blue-400 font-bold px-4 py-4 rounded-lg">BLUE</button>

的数据
1.您可以使用**@Variants**文档创建自己的自定义类,但不能使用CDN的链接。
在使用CDN构建之前,请注意,如果不将Tailwind整合到构建过程中,Tailwind CSS的许多功能将无法使用。documentation

  1. JavaScript解决方案
// Select all elements with `bg-blue-400` class
  const blueBox = document.querySelectorAll('.bg-blue-400');

  const changeColor = ev => {
    // Define button in the DOM
    const button = ev.target.closest('button');
    // Check, if the button was clicked
    if (button) {
      // Chenge color in the DIVs
      for (const box of blueBox) {
        box.classList.add('bg-red-400');
        box.classList.remove('bg-blue-400');
      }
      return;
    }
    // If clicked outside, the color will switch back
    for (const box of blueBox) {
      box.classList.add('bg-blue-400');
      box.classList.remove('bg-red-400');
    }
  };
  document.addEventListener('click', changeColor);
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />

<div>
  <button class="one bg-red-400 font-bold px-4 py-4 rounded-lg">RED</button>
  <div class="bg-blue-400 font-bold px-4 py-4 rounded-lg">BLUE</div>
</div>

<div class="two bg-blue-400 font-bold px-4 py-4 rounded-lg">APPLY COLOR HERE</div>

的字符串

相关问题