0

我正在使用 alpinejs x-for 循环构建一个按钮组,其中第一个和最后一个按钮应该有 rounded-l-md 和 rounded-r-md 使用first:rounded-l-mdand last:rounded-r-mdlast:rounded-r-md工作正常,但是first:rounded-l-md不起作用,因为我认为用于循环的<template>循环被视为第一个元素。

我添加了一个 jsfiddle:https ://jsfiddle.net/20qega7d/

4

1 回答 1

1

我认为是因为模板标签中的第一个元素,但我做了一个扭曲来得到你想要的,我添加了一个,key因为它在x-for. 现在我在 中返回了数组索引for in loop,然后绑定一个类来检查索引是否应该添加rounded-l-md到它。

<!-- Include CDN JavaScript -->
<script src="https://unpkg.com/tailwindcss-jit-cdn"></script>

<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>

<!-- Specify a custom TailwindCSS configuration -->
<script type="tailwind-config">
  {
    variants: {
        extend: {
            borderRadius: ['last, first'],
        },
    },
}
</script>

<div class="p-10" x-data="{ timeframes: [{id: 1, n: '1d'}, {id: 2, n: '1w'}, {id: 3, n: '1y'}] }">
  <span class="relative z-0 inline-flex shadow-sm rounded-md">
    <template x-for="(timeframe, index) in timeframes" :key="timeframe.id">
    <button type="button" class="-ml-px relative inline-flex items-center px-4 py-2 last:rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500"
    :class="{'rounded-l-md' : !index}"
    >
        <span x-text="timeframe.n"></span>
      </button>      
    </template>
  </span>
</div>

于 2021-11-22T09:52:14.037 回答