Use of v-on:clock in VueI'm currently learning the vue.js framework. Make a note of any problems you encounter so you can refer to them later. First, here is a page (for easier observation, each label is colored one by one): The html code is: <div class="groupbody"> <ul class="list"> <li v-for="cell in todo.groupbody " class="pagegroupcell " v-on:click="exchange($event)"> <div class="pagecelltext">{{ cell.left }}</div> <div class="pagecellmin">{{ cell.min }}</div> <img src="img/images/direction/right.png" style="width: 20px;height: 30px;float: right;margin-top: 5px;"> <div class="pagecellmsg">{{ cell.right }}</div> </li> </ul> <div class="clear"></div> </div> js part of the code: exchange: function (event) { alert("Start executing method"); var a = event.target; var cellimg = a.getElementsByTagName("div")[0]; var msg = cellimg.innerText; page2datas.todos[0].groupheader = msg; alert("Method is executing"); var b = document.getElementById("page1"); b.style.display = "none"; var c = document.getElementById("page2"); c.style.display = "block"; alert("Method execution ended"); } At this time, if you click the colored area in the cell (that is, when you click the text label of the li tag), only the first alert() method is executed, and the following two methods are not executed. The reason is the event parameter of this method:If there is a parameter $event in the tag binding method, you can use event.target to get the tag that is bound to the click event at the current point. However, this parameter may also cause some problems. For example, if you want to execute the click event method completely regardless of which part of the li tag is clicked, the event parameter is not applicable. At this time, we can only think of other solutions. Solution:There is a v-for loop in li, which has a cell object, which contains the data in the li tag. You only need to pass this object to the click event method, and you can use this object to achieve the purpose you just wanted to achieve. The modified html code: <div class="groupbody"> <ul class="list"> <li v-for="cell in todo.groupbody " class="pagegroupcell " v-on:click="exchange(cell)"> <div class="pagecelltext">{{ cell.left }}</div> <div class="pagecellmin">{{ cell.min }}</div> <img src="img/images/direction/right.png" style="width: 20px;height: 30px;float: right;margin-top: 5px;"> <div class="pagecellmsg">{{ cell.right }}</div> </li> </ul> <div class="clear"></div> </div> The modified js code: exchange: function (cell) { alert("Start executing method"); page2datas.todos[0].groupheader =cell.left; alert("Executing"); var b = document.getElementById("page1"); b.style.display = "none"; var c = document.getElementById("page2"); c.style.display = "block"; alert("Method execution ended"); } At this point, the entire electric shock event execution method can be fully executed, and subsequent page switching can also be completed. Supplement: Parameter issues when binding click events to v-on in Vue There are three cases regarding parameters when binding click events to v-on, as follows: There are no parentheses after the bound method<button @click="btnClick">Click</button> <script> const app = new Vue({ methods:{ btnClick(event){ // At this time, event is the object currently clicked console.log(event) } } }) </script> The bound method is followed by parentheses<button @click="btnClick()">Click</button> <script> const app = new Vue({ methods:{ btnClick(event){ // At this time event is undefined console.log(event) } } }) </script> The bound method has parentheses after it, and parameters need to be passed<button @click="btnClick(123)">Click</button> <script> const app = new Vue({ methods:{ btnClick(event){ // At this time, event is 123 console.log(event) } } }) </script> The bound method has parentheses after it, and it needs to pass parameters and the currently clicked object.<!-- In this case, if you need to pass the currently clicked object, the parameter must be $event --> <!-- If you want to pass a number in the first position, you don't need to add quotation marks. If you want to pass a string, you must add quotation marks. If you don't add quotation marks, Vue will parse it as a variable. If it is not defined in data, an error will be reported. --> <button @click="btnClick(123,$event)">Click</button> <script> const app = new Vue({ methods:{ btnClick(num,event){ // At this time, num is 123, event is the object currently clicked, console.log(num,event) } } }) </script> The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me. You may also be interested in:
|
<<: Use iptables and firewalld tools to manage Linux firewall connection rules
>>: Problems encountered by MySQL nested transactions
http://www.cppcns.com/shujuku/mysql/283231.html Y...
Preface When I was working on a project recently,...
Introduction to border properties border property...
today select * from table name where to_days(time...
Table of contents 1. Communication method between...
When I used g++ to compile the cpp file for the f...
Many people have read this book: "Grow as a ...
emmm the name is just a random guess 2333 Preface...
1. Design source code Copy code The code is as fol...
The table caption can be placed above or below th...
Screen Introduction Screen is a free software dev...
Docker underlying technology: The two core techno...
This article describes how to install MySQL 5.7 f...
grammar background: linear-gradient(direction,col...