简体   繁体   English

使用点击突出显示构建 vue.js 调查

[英]Build vue.js survey with click highlighting

I try to create a survey using vue.js , which is great.我尝试使用vue.js创建一个调查,这很棒。 In each section the user is able to select an answer by clicking the div.box .在每个部分中,用户都可以通过单击div.box来选择答案。 After a click event I want to things to happen:在点击事件之后,我希望事情发生:

  1. Assign data-value="" of clicked box to the data: layer of my app as selected answer将单击框的data-value=""分配给data:我的应用程序层作为选定答案

  2. Mark the clicked div.box by adding .choosen , while the same class should be removed of all other boxes in the same section.通过添加.choosen标记单击的div.box ,同时应从同一部分中的所有其他框中删除相同的类。

I got my HTML markup like this:我得到了这样的 HTML 标记:

<div class="wrapper">
  <div class="box-holder">
    <div class="box" data-value="lorem">Dogs</div>
  </div>
  <div class="box-holder">
    <div class="box" data-value="aperiam">Birds</div>
  </div> 
  ... and some more ...
</div>

In jQuery I could simply create a $('.box') selector and use .parent() with removeClass() and addClass() .在 jQuery 中,我可以简单地创建一个$('.box')选择器并将.parent()与 removeClass( removeClass()addClass()一起使用。 How can I achieve the same in vuejs?如何在 vuejs 中实现相同的功能?

JS FIDDLE DEMO JS 小提琴演示

 new Vue({ el: ".wrapper", data: { list: [ { id: 'lorem', text: 'Dogs' }, { id: 'aperiam', text: 'Birds' }, { id: 'aperiam2', text: 'Birds2' }, { id: 'aperiam3', text: 'Birds3' } ], choosen: 'lorem' }, methods: { onClick: function(id) { this.choosen = id; } } })
 .choosen{ color: red }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script> <div class="wrapper"> <div class="box-holder" v-for="item in list" :class="{ 'choosen': choosen === item.id }" > <div class="box" @click="onClick(item.id)">{{ item.text }}</div> </div> </div>

You need to add a click handler to your element, and a conditional class binding.您需要为您的元素添加一个点击处理程序和一个条件类绑定。 For example:例如:

<div class="box" data-value="lorem" 
  v-on:click="favoritePet = 'dogs' "
  v-bind:class="{ choosen: favoritePet == 'dogs' }"
>Dogs</div>

Docs on binding classes here : https://v2.vuejs.org/v2/guide/class-and-style.html#Object-Syntax And event handling here : https://v2.vuejs.org/v2/guide/events.html关于绑定类的文档: https ://v2.vuejs.org/v2/guide/class-and-style.html#Object-Syntax 和事件处理: https ://v2.vuejs.org/v2/guide/事件.html

Use component使用组件

 Vue.component('question', { template: '#question', data: function() { return { choosen: undefined } }, props: ['data'], methods: { onClick: function(choosen) { this.choosen = choosen; this.$emit('set-question', this.choosen, this.data.id) } } }) new Vue({ el: "#app", data: { question: {}, question1: { id: 'question1', list: [ { id: 'lorem', text: 'Dogs' }, { id: 'aperiam', text: 'Birds' }, { id: 'aperiam2', text: 'Birds2' }, { id: 'aperiam3', text: 'Birds3' } ], title: 'Your favorite pet?' }, question2: { id: 'question2', list: [ { id: 'lorem', text: 'Dogs' }, { id: 'aperiam', text: 'Birds' }, { id: 'aperiam2', text: 'Birds2' }, { id: 'aperiam3', text: 'Birds3' } ], title: 'Your favorite sport?' } }, methods: { setQuestion: function(choosen, id) { this.question[id] = choosen; console.log(this.question); } } })
 .choosen{ color: red } #app{ padding-bottom: 200px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script> <div id="app"> <question :data="question1" @set-question="setQuestion"></question> <question :data="question2" @set-question="setQuestion"></question> </div> <template id="question"> <div> <h1>{{data.title}}</h1> <div class="wrapper"> <div class="box-holder" v-for="item in data.list" :class="{ 'choosen': choosen === item.id }" > <div class="box" @click="onClick(item.id)">{{ item.text }}</div> </div> </div> </div> </template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM