猿问

vue 如何实现 找到好多li标签中的某一个?

现在试着做一个日期选择器,当我点击input框弹出日期选择面板时,当天日期应该是默认选中的,即给这一天的dom加一个active的class。

按照以前jquery的思路,我给每天的dom加一个data-day='20170101'属性,然后使用$('li[data-day=20170101]')这样就能找到这个元素。

可是在vue中我应该从哪个方向去思考?应该怎么做?就是不通过元素查找的方式,而是通过比如computed之类的方法来改变状态什么的来实现


临摹微笑
浏览 2192回答 1
1回答

牛魔王的故事

代码应该如下(大概的示例):<script>export default {&nbsp; &nbsp; data () {&nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tody: TODAY,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; days: [/*数据在这里*/],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activeIndex: null&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; methods: {&nbsp; &nbsp; &nbsp; &nbsp; select (index) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.activeIndex = index&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}</script><template><ul>&nbsp; &nbsp; <li v-for="(day, index) in days"&nbsp; &nbsp; @click="select(index)"&nbsp; &nbsp; :class="{active: index === activeIndex, today: day === today}">&nbsp; &nbsp; {{day}}&nbsp; &nbsp; </li></ul><template>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答