Vue中的@blur/@focus事件怎么使用

其他教程   发布日期:2023年08月06日   浏览次数:459

这篇文章主要讲解了“Vue中的@blur/@focus事件怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Vue中的@blur/@focus事件怎么使用”吧!

Vue的@blur/@focus事件

    1. @blur
    是当元素失去焦点时所触发的事件
    1. @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. switch (flag) {
  3. case 1:
  4. console.log('获取焦距')
  5. this.$refs.searchBoxOfChatRoom.style.background = 'white'
  6. this.$refs.searchOfChatRoom.style.background = 'white'
  7. break
  8. case 2:
  9. console.log('失去焦距')
  10. this.$refs.searchBoxOfChatRoom.style.background = '#dadada'
  11. this.$refs.searchOfChatRoom.style.background = '#dadada'
  12. break
  13. default:
  14. break
  15. }
  16. }

以上就是Vue中的@blur/@focus事件怎么使用的详细内容,更多关于Vue中的@blur/@focus事件怎么使用的资料请关注九品源码其它相关文章!