【问题标题】:Vue 2 toggle class for below element以下元素的Vue 2切换类
【发布时间】:2017-05-02 00:30:38
【问题描述】:

我正在尝试在点击时切换另一个 div 的类。

类似:

<div class="link">
 <p> Click here to show the content </p>
 <div class="content">
  <p>This is the hidden content</p>
 </div>
</div>

所以内容 css 最初应该是:显示:无

如何在 vue 上做到这一点,当点击 p 时,切换下面的元素。

提前谢谢!

【问题讨论】:

    标签: javascript html css vue.js vuejs2


    【解决方案1】:

    如果您使用v-show directive,则不需要为此使用 css 类:

    <div class="link">
      <p @click="show = true"> Click here to show the content </p>
      <div v-show="show" class="content">
        <p>This is the hidden content</p>
      </div>
    </div>
    

    在您的 Vue 组件中,您只需添加一个初始设置为 falseshow 属性:

    data() {
      return {
        show: false,
      };
    }
    

    【讨论】:

    • 谢谢...跨度>
    • 非常感谢您的帮助,我还有其他相关问题.. 我有许多相同的 div,大约 10 个,我希望能够在单击
    • 我会把它变成它自己的可重用组件:vuejs.org/v2/guide/components.html
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签