Home > Web Front-end > JS Tutorial > body text

How to implement the selected change method using vue.js

亚连
Release: 2018-06-07 11:10:05
Original
1744 people have browsed it

Below I will share with you an article on how to use vue.js to change the selected state. It has a good reference value and I hope it will be helpful to everyone.

After using the prototype to change the unselected state, I came into contact with vue and wondered if I could use vue to implement the function. The page in the previous article did not dynamically implement the page, and all the data was not They are all written directly in html. After using vue, it has been possible to dynamically generate pages based on the amount of data. And the amount of code is also greatly reduced.

html part code:

<p data-role="page " class="page "> 
 <p class="center " id="app"> 
 <p class="group "> 
 <ul> 
 <li v-for = "todo in todos "> 
  <p class="groupheader "> 
  <p class="Gheadertext ">{{todo.groupheader}}</p> 
  </p> 
  <p class = "groupbody "> 
  <ul class="list "> 
  <li v-for="cell in todo.groupbody" v-on:click="exchange($event)" class="groupcell"> 
  <p class="celltext"> 
   {{ cell.text }} 
  </p> 
  <img class="selectimg" src="img/select.png "> 
  </li> 
  </ul> 
  </p> 
  </li> 
 </ul> 
 </p> 
 </p> 
</p>
Copy after login

Data code:

var datas = { 
 todos :[ 
 { 
 groupheader : &#39;MB3101&#39;, 
 groupbody:[ 
  { text: &#39;调整不当&#39;}, 
  { text: &#39;光电开关损坏&#39; }, 
  { text: &#39;镜面积灰&#39; }, 
  { text: &#39;调整不当&#39; }, 
  { text: &#39;光电开关损坏&#39; }, 
  { text: &#39;镜面积灰&#39; }, 
  { text: &#39;调整不当&#39; }, 
  { text: &#39;光电开关损坏&#39; }, 
  { text: &#39;镜面积灰&#39; }, 
 ] 
 }, 
 { 
 groupheader : &#39;MB3102&#39;, 
 groupbody:[ 
  { text: &#39;调整不当&#39; }, 
  { text: &#39;光电开关损坏&#39; }, 
  { text: &#39;镜面积灰&#39; }, 
  { text: &#39;调整不当&#39; }, 
  { text: &#39;光电开关损坏&#39; }, 
  { text: &#39;镜面积灰&#39; }, 
  { text: &#39;调整不当&#39; }, 
  { text: &#39;光电开关损坏&#39; }, 
  { text: &#39;镜面积灰&#39; }, 
 ] 
 }, 
 { 
 groupheader : &#39;MB3103&#39;, 
 groupbody:[ 
  { text: &#39;调整不当&#39; }, 
  { text: &#39;光电开关损坏&#39; }, 
  { text: &#39;镜面积灰&#39; }, 
  { text: &#39;调整不当&#39; }, 
  { text: &#39;光电开关损坏&#39; }, 
  { text: &#39;镜面积灰&#39; }, 
  { text: &#39;调整不当&#39; }, 
  { text: &#39;光电开关损坏&#39; }, 
  { text: &#39;镜面积灰&#39; }, 
 ] 
 } 
 ] 
}
Copy after login

The code of the js part:

new Vue({ 
 el: &#39;#app&#39;, 
 data:datas, 
 methods:{ 
 exchange:function(event){ 
  //获取被点击的元素对象 
  var a = event.target; 
  //获取被点击元素中的子元素<img> 
  var cellimg = a.getElementsByTagName("img")[0]; 
  if(a.className == "groupcell") { 
  a.className = "selectcell"; 
  cellimg.style.display = "block"; 
 } 
 else if(a.className == "selectcell") { 
  a.className = "groupcell"; 
  cellimg.style.display = "none"; 
 } 
 } 
 } 
})
Copy after login

The effect is as shown in the picture:

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to use refs in React components

Cross-domain issues with proxyTable in the vue-cli project

express build query server

The above is the detailed content of How to implement the selected change method using vue.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template