经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
Vue中的@blur/@focus事件解读
来源:jb51  时间:2023/3/6 9:12:53  对本文有异议

Vue的@blur/@focus事件

  • @blur 是当元素失去焦点时所触发的事件
  • @focus是元素获取焦点时所触发的事件
  1. <template>
  2. <div>
  3. <!--
  4. @blur 当元素失去焦点时触发blur事件
  5. -->
  6. <div>
  7. <input type="text" placeholder="请输入内容" @blur="blurText"/>
  8. </div>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. name: "commitText",
  14. methods:{
  15. blurText(){
  16. console.log("blur事件被执行了")
  17. }
  18. }
  19. }
  20. </script>
  21. <style scoped>
  22. </style>

focus和blur事件,改变选中时搜索框的背景色

template

  1. <div class="search-box" ref="searchBoxOfChatRoom">
  2. ?? ?<i class="fa fa-search" aria-hidden="true"></i>
  3. ?? ?<input
  4. ?? ??? ?ref="searchOfChatRoom"
  5. ?? ??? ?class="chatroom-search"
  6. ?? ??? ?type="search"
  7. ?? ??? ?placeholder="搜索群成员"
  8. ?? ??? ?@focus="changBackground(1)"
  9. ?? ??? ?@blur="changBackground(2)"
  10. ?? ?>
  11. </div>

js

  1. changBackground (flag) {
  2.  
  3. ? switch (flag) {
  4. ? ? case 1:
  5. ? ? ? console.log('获取焦距')
  6. ? ? ? this.$refs.searchBoxOfChatRoom.style.background = 'white'
  7. ? ? ? this.$refs.searchOfChatRoom.style.background = 'white'
  8. ? ? ? break
  9. ? ? case 2:
  10. ? ? ? console.log('失去焦距')
  11. ? ? ? this.$refs.searchBoxOfChatRoom.style.background = '#dadada'
  12. ? ? ? this.$refs.searchOfChatRoom.style.background = '#dadada'
  13. ? ? ? break
  14. ? ? default:
  15. ? ? ? break
  16. ? }
  17. }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号