vue3+bootstrap5中,v-for与click一起用时事件无效

作者分享了解决Vue项目中v-for与@click配合Bootstrap5list-group导致点击事件失效的问题,通过将list-group转换为table并保持事件绑定,确保了功能正常工作。还提及了防止table宽度变化的CSS样式。
摘要由CSDN通过智能技术生成

遇到的问题

最开始我以为我遇到的问题:

v-for 和 @click一起用导致click事件无效

然后在网上搜了好久,试了很多解决方案,但是都没有成功

实际上我遇到的问题:

我的v-for写在bootstrap5的list-group的li标签上,而该标签以及子标签是无法触发click事件的

解决方案

把列表组list-group改成表格table(同样也是bootstrap5中的)

最后代码为:

<main>
      <!-- 表格 -->
      <table class="table table-hover">
        <tbody>
          <!-- 一行 -->
          <tr v-for="(item,index) in list" :key="item.userid" >
            <!-- 用户名 -->
            <td @click="doclick('用户详细')">
              <span>{{ item.username }}</span>
            </td>
            <!-- 按钮 -->
            <td class="td-btn" v-if="item.isFocus">
              <button @click.stop="unFocus(index,item.userid)" 
					    type="button" class="btn btn-secondary">
						    已相互关注
					    </button>
            </td>
            <td class="td-btn" v-else>
              <button @click.stop="focus(index,item.userid)"  
					    type="button" class="btn btn-primary">
						    回关
					    </button>
            </td>
          </tr>
        </tbody>
      </table>
    </main>

其中防止table被内容撑开改变宽度的style为

table{
  table-layout: fixed;
  word-break: break-all;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值