博客列表 >条件渲染和事件绑定

条件渲染和事件绑定

P粉036614676
P粉036614676原创
2022年09月05日 15:01:16333浏览

1.列表渲染

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>列表渲染</title>
  8. <script src="https://unpkg.com/vue@3"></script>
  9. <style>
  10. li::marker{
  11. content: none;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!-- 1.原生:<script>
  17. const ul = document.createElement('ul');
  18. const arr = ['wuhan','beijing','congqin'];
  19. for(let i of arr)
  20. {
  21. let li = document.createElement('li');
  22. li.append(i);
  23. ul.append(li);
  24. }
  25. document.body.append(ul);
  26. </script> -->
  27. <!--Vue 1: <div class="app">
  28. <ul>
  29. <li>{{City[0]}}</li>
  30. <li>{{City[1]}}</li>
  31. <li>{{City[2]}}</li>
  32. </ul>
  33. </div> -->
  34. <hr>
  35. <div class="app">
  36. <ul>
  37. <!-- <li v-for="(city,index) of City" :key = "index">{{city}}</li> -->
  38. <!-- 上下相等<li v-for="city of City">{{city}}</li> -->
  39. <li v-for="(message) of Messages">{{message['name']}}:{{message['email']}}</li>
  40. </ul>
  41. </div>
  42. <script>
  43. Vue.createApp({
  44. data(){
  45. return {
  46. City:['wuhan','beijing','congqin'],
  47. Messages:[
  48. {name:'yk',email:'yk@.com'},
  49. {name:'yk',email:'yk@.com'},
  50. { name:'yk',email:'yk@.com'}
  51. ]
  52. }
  53. }
  54. }).mount('.app');
  55. </script>
  56. </body>
  57. </html>

2.双向绑定

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>双向绑定</title>
  8. <script src="https://unpkg.com/vue@3"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <p>
  13. <input type="text" v-model = "comment" :value="comment">
  14. <span>{{comment}}</span>
  15. </p>
  16. </div>
  17. <script>
  18. const app = Vue.createApp({
  19. data(){
  20. return {
  21. comment :null,
  22. }
  23. }
  24. }).mount('.app');
  25. </script>
  26. </body>
  27. </html>

3.事件绑定

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title></title>
  8. <script src="https://unpkg.com/vue@3"></script>
  9. </head>
  10. <body>
  11. <!-- <div class="first">
  12. <a href="www.baidu.com" onclick="showUrl()">Url</a>
  13. <p></p>
  14. </div>
  15. <script>
  16. function showUrl()
  17. {
  18. //事件禁止跳转
  19. event.preventDefault();
  20. event.target.nextElementSibling.textContent = event.target.href;
  21. }
  22. </script> -->
  23. <div class="app">
  24. <a href="www.baidu.com" @click="showUrl($event)">Url</a>
  25. <!-- 等价于:<a href="www.baidu.com" v-on:click="showUrl($event)">Url</a> -->
  26. <p class="url">{{url}}</p>
  27. </div>
  28. <script>
  29. Vue.createApp({
  30. data(){
  31. return {
  32. url:null
  33. }
  34. },
  35. methods:{
  36. showUrl(ev)
  37. {
  38. ev.preventDefault();
  39. ev.stopImmediatePropagation();
  40. this.url = ev.target.href;
  41. }
  42. }
  43. }).mount('.app');
  44. </script>
  45. </body>
  46. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议