首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JavaScript将CSS添加到Vue

使用JavaScript将CSS添加到Vue
EN

Stack Overflow用户
提问于 2021-09-16 12:31:37
回答 1查看 108关注 0票数 1

我试图在<label> Vue上添加样式。

代码语言:javascript
复制
  <div class="grid grid-cols-1 mt-5 mx-7">
      <label class="uppercase md:text-sm text-xs text-gray-500 text-light font-semibold mb-1 flex items-center justify-center">Upload Photo</label>
        <div class='flex items-center justify-center w-full'>    
            <label  id="img1" class='flex flex-col border-4 border-dashed w-40 h-32 hover:bg-gray-100 hover:border-purple-300 group'>
                 <div @click="onPrimaryPicSelected" class='flex flex-col items-center justify-center pt-7'>
                  <svg class="w-10 h-10 text-purple-400 group-hover:text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
                  <p class='lowercase text-sm text-gray-400 group-hover:text-purple-600 pt-1 tracking-wider'>Select a photo</p>
                </div>
              <input type='file' class="hidden" />
            </label>
        </div>
    </div>

当调用onPrimaryPicSelected函数将CSS样式添加到<label>标记时,我希望这样做。如果可能的话,我该怎么做呢?

目前,onPrimaryPIcSelected只获取上传的图像。

代码语言:javascript
复制
 onPrimaryPicSelected(event) {
      this.primaryPhoto = event.target.files[0];
      this.primaryPhotoUrl = URL.createObjectURL(event.target.files[0]);
    },
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-16 12:58:34

您可以使用布尔变量(如果为true ),它将将样式应用于标签标记。

代码语言:javascript
复制
<div class="grid grid-cols-1 mt-5 mx-7">
  <label class="uppercase md:text-sm text-xs text-gray-500 text-light font-semibold mb-1 flex items-center justify-center" :style="selected ? 'your-style-here' : ''">Upload Photo</label>
    <div class='flex items-center justify-center w-full'>    
        <label  id="img1" class='flex flex-col border-4 border-dashed w-40 h-32 hover:bg-gray-100 hover:border-purple-300 group'>
             <div @click="onPrimaryPicSelected" class='flex flex-col items-center justify-center pt-7'>
              <svg class="w-10 h-10 text-purple-400 group-hover:text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
              <p class='lowercase text-sm text-gray-400 group-hover:text-purple-600 pt-1 tracking-wider'>Select a photo</p>
            </div>
          <input type='file' class="hidden" />
        </label>
    </div>
</div>

然后在你的职能中

代码语言:javascript
复制
onPrimaryPicSelected(event) {
  this.selected = true;
  this.primaryPhoto = event.target.files[0];
  this.primaryPhotoUrl = URL.createObjectURL(event.target.files[0]);
},
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69208493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档