javascript - Vue 錯(cuò)誤Uncaught TypeError: todo[i].css is not a function
問題描述
在做一個(gè)todolist,有個(gè)按鈕點(diǎn)擊顯示沒有完成的todolist,思路是點(diǎn)擊按鈕方法遍歷整個(gè)items,完成的item隱藏來達(dá)到顯示全部未完成的item。可是數(shù)組遍歷item可以取到index值,但是無法改變css
<template> <p > <h1>{{title}}</h1> <input v-model=’newItem’ placeholder=’Add your todolist’ class=’inputItem’ v-on:keyup.enter=’addNew’> <p class='nav'> <img src='http://www.gepszalag.com/wenda/assets/all.png'> <img src='http://www.gepszalag.com/wenda/assets/checkBox.png' @click=’showUndo(item)’> <img src='http://www.gepszalag.com/wenda/assets/checkBoxF.png' @click=’showDone(item)’> <img src='http://www.gepszalag.com/wenda/assets/delete.png' @click=’deleteAll(item)’> </p> <ul> <li v-for=’(item,index) in items’ :index='index' class=’todo-line’ ><p class=’view’ @mouseenter=’itemEnter(item)’ @mouseleave=’itemLeave(item)’> <span 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 src='http://www.gepszalag.com/wenda/assets/delete1.png' v-if=’item.showDelete’ @click=’deleteTodo(item)’ ></p> </li> </ul> </p></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>
請(qǐng)問是哪里出錯(cuò)了嗎??真心求助
問題解答
回答1: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;}回答2:
vue的思想是盡量少操作DOM,盡可能的只通過改變數(shù)據(jù)來改變視圖;如果想實(shí)現(xiàn)點(diǎn)擊按鈕時(shí)切換顯示對(duì)應(yīng)狀態(tài)的item,可以用計(jì)算屬性來篩選出對(duì)應(yīng)的數(shù)據(jù)
回答3:Vue 提供了通過數(shù)據(jù)綁定樣式的方案,因此其余回答都是不準(zhǔn)確的。
<template> <p : : > Demo </p></template><script>export default { data () { return { demo: { isActive: false, color: ’red’ } } }}</script>回答4:
一樓正解,vue應(yīng)用中盡量采用數(shù)據(jù)驅(qū)動(dòng)的開發(fā)模式,減少dom的操作。在DOM中關(guān)聯(lián)的數(shù)據(jù)在data中提前初始化,或是在computed中有個(gè)處理,整個(gè)業(yè)務(wù)邏輯中僅僅是操作數(shù)據(jù),從而達(dá)到響應(yīng)式更新dom的目的。樓主出現(xiàn)的這個(gè)錯(cuò)誤很明顯是你的設(shè)置css的方式錯(cuò)了,去查查js或者jquery如何更改css。
相關(guān)文章:
1. node.js - vue2 的npm run dev 卡死... 網(wǎng)頁無法打開. 8080端口沒有被占用...2. phpstady在win10上運(yùn)行3. vim - docker中新的ubuntu12.04鏡像,運(yùn)行vi提示,找不到命名.4. 用PHP怎么在微信公眾號(hào)里使用模板信息,公眾號(hào)還未做開發(fā)?5. index.php錯(cuò)誤,求指點(diǎn)6. 微信無法掃描phpqrcode生成的二維碼7. javascript - 百度echarts圖表如何修改8. node.js - vue-cll+sass 樣式不出來 已經(jīng)npm install sass、 sass-loader了9. python - 在ubuntu下安裝scrapy過程遇到的問題10. python2.7 - django 無法連接redis
