javascript - 以下元素的 Vue 2 切换类

标签 javascript html css vue.js vuejs2

我试图在点击时切换另一个 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 最初应该是:display: none

我怎么能在vue上做到这一点,当p被点击时,切换下面的元素。

提前致谢!

最佳答案

如果您使用 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,
  };
}

关于javascript - 以下元素的 Vue 2 切换类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43728361/

相关文章:

jquery - 有什么方法可以将类添加到 JOOMLA PLUGIN

javascript - 单击时菜单的水平移动

css - Bootstrap 4 全宽轮播显示其他幻灯片

php - CSS形式的照片方向

javascript - 具有 Stack Exchange 基本信息的投资组合

javascript - 从对象中删除所有符号的最有效方法是什么?

javascript - 如何在 ProtoType 中使用 Promises *around* 回调进行方法链接

javascript - 如何使用 JQuery 从相对元素获取 html 文本?

html - Mac 上浏览器之间的字体大小不一致

html - 为简单的响应式 header 编写代码