你可以使用TailwindCSS来动态更新元素的颜色值吗?

huangapple go评论67阅读模式
英文:

How can I use TailwindCSS to dynamically update an element's color value?

问题

I understand your request. Here is the translated content:

我有一个网站,在这个网站上,我希望用户能够使用TailwindCSS动态更新一个div的颜色。但是,当我添加一个class时,似乎Tailwind并没有反映出这个改变。

请注意,尽管提供的示例在这里可以工作,但并不反映出一个正确安装了Tailwind依赖的真实行为。这是因为Tailwind的(按照说明安装)class是在编译时定义的,而CDN是在运行时构建的。基本上,CDN会在DOM中的每一次改变时更新,而Tailwind的“prod”是根据对html/js/css文件的简单分析创建的。

我选择包含CDN版本。这是因为它包含了我应用程序所需的行为,并且是我唯一能够包含的版本。如果你想重现问题的行为,只需将代码复制到一个普通的Tailwinds安装项目中,并且删除<script src="https://cdn.tailwindcss.com"></script>这一行。

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

document.querySelector("button").addEventListener("click", function() {
  const colorValue = document.querySelector("input").value;
  const div = document.querySelector("div");
  div.classList.remove("bg-[#864b4b]");
  div.classList.add(`bg-[${colorValue}]`);
});

<!-- 语言: lang-html -->

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>示例问题</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="ml-4 h-96">
  <label for="color-pick" class="block font-extrabold text-slate-800">选择一个颜色</label>;
  <input type="color" class="mb-4" name="color-pick" id="color-pick" />;
  <button
    type="button"
    class="block text-slate-100 font-bold bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 rounded-lg text-sm px-5 py-2.5 mr-2 mb-2"
  >
    将div改为所选颜色
  </button>;
  <div class="w-20 h-20 bg-[#864b4b]"></div>
</body>

</html>

<!-- 结束代码片段 -->

在我看来,唯一的方法是通过使用style属性编辑元素的样式。然而,如果有一种Tailwind的方法来做到这一点,那可能会更合适。

英文:

I have a website where I want a user to be able to update a div's color dynamically using TailwindCSS. However, when I add a class, it seems that Tailwind does not reflect the change.

Please note that the example provided, although it does work here, does not reflect the true behavior of a properly installed Tailwind dependency. This is because Tailwind's (as installed per instructions) classes are defined at compile time, while the cdn is built at runtime. Basically, the CDN is updated for every change in the DOM, while Tailwinds "prod" is created based on a simple analysis of the html/js/css files.

I've elected to include the CDN version. This is included here because it contains the desired behaviour for my application, and is the only version I can include. If you'd like to reproduce the problem behavior, simply copy the code into a normal Tailwinds installation project and edit out the &lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt; line.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.querySelector(&quot;button&quot;).addEventListener(&quot;click&quot;, function() {
  const colorValue = document.querySelector(&quot;input&quot;).value;
  const div = document.querySelector(&quot;div&quot;);
  div.classList.remove(&quot;bg-[#864b4b]&quot;);
  div.classList.add(`bg-[${colorValue}]`);
});

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot; /&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  &lt;title&gt;Example issue&lt;/title&gt;
  &lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body class=&quot;ml-4 h-96&quot;&gt;
  &lt;label for=&quot;color-pick&quot; class=&quot;block font-extrabold text-slate-800&quot;&gt;Pick a color&lt;/label
    &gt;
    &lt;input type=&quot;color&quot; class=&quot;mb-4&quot; name=&quot;color-pick&quot; id=&quot;color-pick&quot; /&gt;
    &lt;button
      type=&quot;button&quot;
      class=&quot;block text-slate-100 font-bold bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 rounded-lg text-sm px-5 py-2.5 mr-2 mb-2&quot;
    &gt;
      Change div to chosen color
    &lt;/button&gt;
    &lt;div class=&quot;w-20 h-20 bg-[#864b4b]&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

In my opinion, the only this can be done is by editing the style of the element by using the style attribute. However, if there is a Tailwinds way to do it, it would probably be more appropriate.

答案1

得分: 0

考虑使用style属性:

document.querySelector("button").addEventListener("click", function() {
  const colorValue = document.querySelector("input").value;
  const div = document.querySelector("div");
  div.classList.remove("bg-[#864b4b]");
  div.style.backgroundColor = colorValue;
});
英文:

Consider using the style attribute:

document.querySelector(&quot;button&quot;).addEventListener(&quot;click&quot;, function() {
  const colorValue = document.querySelector(&quot;input&quot;).value;
  const div = document.querySelector(&quot;div&quot;);
  div.classList.remove(&quot;bg-[#864b4b]&quot;);
  div.style.backgroundColor = colorValue;
});

huangapple
  • 本文由 发表于 2023年5月22日 00:02:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定