vue input 点击文本变输入框,失焦后变回文本,解决点击文本时自动获取焦点

最开始效果是不对的,点击一行单元格,虽然由文本变为输入框了,但是当点击第二行的输入框时,第一行并没有失焦,因为失焦事件加在了input上,当点击时input 并没有聚焦,所以当点击第二个时也没走失焦事件

原代码

    <el-table
      :data="tableData"
      border
      :header-cell-style="{background:'#f5f7fa',color:'#909399'}"
      style="width: 100%"
      class="table-style">
      <el-table-column prop="date" label="日期" align="center"></el-table-column>
      <el-table-column prop="name" label="姓名" align="center">
        <template slot-scope="scope">
          <span
            v-if="!scope.row.cellNameVisible"
            @click="cellClick(scope.row, 'cellNameVisible')"
          >{{ scope.row.name }}</span>

          <el-input
            v-if="scope.row.cellNameVisible"
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 9999}"
            v-model="scope.row.name"
            @blur="blurEvent(scope.row,'cellNameVisible')"
          >
          </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="address" align="center" label="地址"></el-table-column>
    </el-table>

 

// data中
      tableData: [{
        id: 1,
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄',
        cellNameVisible: false,
      }, {
        id: 2,
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄',
        cellNameVisible: false,
      }, {
        id: 3,
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄',
        cellNameVisible: false,
      }, {
        id: 4,
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄',
        cellNameVisible: false,
      }]
methods
    /**
     * 点击文本触发,显示input框,隐藏span标签
     */
    cellClick(row, cellIndex) {
      this.$nextTick(() => {
         this.$refs.inputFocus.focus();
      });
      row.cellNameVisible = true;
    },
    /**
     * 输入框失去焦点触发
     */
    blurEvent(row, cellIndex) {
      row.cellNameVisible = false;
    },

运行代码发现效果不对如第一张图所示还是没聚焦,开始确认是否是使用v-if导致的,换成v-show,还是不行,最后找百度,结合之前的代码, 终于找到了原因,渲染组件需要时间,但是时间没有JS执行的快;所以获取不到。

方法一:加个指令 v-focus

<span
   v-if="!scope.row.cellNameVisible"
   @click="cellClick(scope.row, 'cellNameVisible')"
   >{{ scope.row.name }}</span>

   <el-input
      ref="inputFocus"
      v-if="scope.row.cellNameVisible"
      type="textarea"
      :autosize="{ minRows: 1, maxRows: 9999}"
      v-model="scope.row.name"
      @blur="blurEvent(scope.row,'cellNameVisible')"
      v-focus/>
    /**
     * 点击文本触发,显示input框,隐藏span标签
     */
    cellClick(row, cellIndex) {
      row.cellNameVisible = true;
    },
    /**
     * 输入框失去焦点触发
     */
    blurEvent(row, cellIndex) {
      row.cellNameVisible = false;
    },

方法二: 使用setTimeout

data中加一个属性 

timer: null,
    /**
     * 输入框失去焦点触发
     */
    blurEvent(row){
      row.cellNameVisible = false;
    },
    /**
     * 点击文本触发,显示input框,隐藏span标签
     */
    cellEdit(row){
      if (this.timer) {
        clearTimeout(this.timer);
      }
      this.timer = setTimeout(() => {
        this.$refs.inputFocus.focus();
      }, 10);
      row.cellNameVisible = true;
    },

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue3中,如果点击输入框以外的地方没有触发失焦事件,而只有再次点击并聚焦输入框,再点击输入框以外的地方,失焦事件才能触发,这可能是因为在模板中使用了条件渲染生成的输入框,并没有通过聚焦事件focus触发。 要解决这个问题,你可以在输入框上添加ref属性,并通过该ref属性在Vue组件中获取输入框的DOM元素。例如,在模板中给输入框添加ref属性:`<input class="ring-2" ref="nameInput" v-else v-model="name" type="text" @blur="submitEditName" @keyup.enter="submitEditName" />` 然后,在Vue组件的方法中,可以通过`this.$refs.nameInput`来获取输入框的DOM元素,并在需要的候进行操作。比如,你可以在失焦事件blur中调用提交编辑的方法submitEditName,代码如下: ```javascript methods: { submitEditName() { // 处理提交编辑的逻辑 } } ``` 这样,无论是点击输入框以外的地方还是按下回车键,都会触发失焦事件,从而调用提交编辑的方法。这样就可以实现输入框失焦的功能了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【Vue实践】VueInput失焦事件无效问题解决](https://blog.csdn.net/baidu_36511315/article/details/118516072)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值