javascript - Vue 错误Uncaught TypeError: todo[i].css is not a function

查看:175
本文介绍了javascript - Vue 错误Uncaught TypeError: todo[i].css is not a function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在做一个todolist,有个按钮点击显示没有完成的todolist,思路是点击按钮方法遍历整个items,完成的item隐藏来达到显示全部未完成的item。
可是数组遍历item可以取到index值,但是无法改变css

<template>
  <div id="app" >
    <h1>{{title}}</h1>
    <input v-model='newItem' placeholder='Add your todolist' class='inputItem' v-on:keyup.enter='addNew'>
    <div class="nav">
      <img src="./assets/all.png">
      <img src="./assets/checkBox.png" @click='showUndo(item)'>
      <img src="./assets/checkBoxF.png" @click='showDone(item)'>
      <img src="./assets/delete.png" @click='deleteAll(item)'>
    </div>
    <ul>
      <li v-for='(item,index) in items' :index="index" class='todo-line'  >
        <div class='view'  @mouseenter='itemEnter(item)' @mouseleave='itemLeave(item)'>
          <span class="checkBox" 
          v-on:click='toggleFinish(item)' v-bind:class='{checked:item.isFinish}'></span>
          <label class='item-label' v-bind:class='{finished:item.isFinish}'>{{ index + 1}} . {{item.label}}</label>
          <img class="item-delete" src="./assets/delete1.png"  v-if='item.showDelete'  @click='deleteTodo(item)' >
        </div>        
      </li>
    </ul>
  </div>
</template>

<script>
import Store from './store'
export default {
  data: function () {
    return {
      item :'',
      title: 'todolist',  
      items: Store.fetch(),
      newItem: ''
    }
  },
   watch: {
    items: {
        handler: function (items) {
        Store.save(items)
      },
      deep: true
    }
  },
  methods: {
      toggleFinish(item) {
      item.isFinish = !item.isFinish
    },
    addNew: function () {
      this.items.push({
        label: this.newItem,
        isFinish: false,
        showDelete:false,
      })
      this.newItem = ''
    },
    itemEnter(item){
      item.showDelete = true
    },
    itemLeave(item){
      item.showDelete = false
    },
    deleteTodo(index){     
      this.items.splice(index,1)
    },
    deleteAll(item){
      this.items.splice(item)
    },
    showUndo(){
      var todo = this.items;
      for (var i = 0;i < todo.length; i++) {
        if (todo[i].isFinish == true) {
          todo[i].css('display', 'none');
          console.log(i)
        }
      }   
    },
  }
}
</script>

请问是哪里出错了吗??真心求助

解决方案

todo[i].css('display', 'none'); 这是JQuery改变css样式的方法呀
贴出来的代码中也没有看到你有引用JQuery

Vue中如果没有引用JQuery,只能用原生JS来修改css样式

如:

todo[i].style.display = 'none'

// 或

todo[i].setAttribute('display', 'none')

// 或

todo[i].className = 'newClass'

.newClass {
    display: none;
}

这篇关于javascript - Vue 错误Uncaught TypeError: todo[i].css is not a function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆