【问题标题】:Change the font size and color of first row of table更改表格第一行的字体大小和颜色
【发布时间】:2019-09-24 01:54:26
【问题描述】:

我想要显示板球队的详细信息。表格的第一行是船长,所以对于每个第一行颜色,我想让字体大小为绿色和粗体

这是我的 HTML:

 <div >
     <table id="sports" border=1 align=center >
         <thead >
         <tr>
             <th>Team1</th>
             <th>Team2</th>
         </tr>
         </thead>
         <tbody  *ngFor="let T of Teams" >
                 <td> {{T.Indian_players}}</td>
                 <td>{{T.Australia_players}}</td>
             </tr>
         </tbody>
     </table>
</div>

这是我的模型,我在其中获取了静态数据:

export const Players=[
        {Indian_players:'Kohili(c)',Australia_players:'David Warner(c)',Pakistan_players:'shaheen Afridi(c)',Southafrica_players:'dale steyn(c)',England_players:'Harry Kane(c)'},
        {Indian_players:' Dhoni',Australia_players:'Steve Smith',Pakistan_players:'sarfraz Ahmed',Southafrica_players:'du plessis',England_players:'Joe Root'},
        {Indian_players:'Rohit Sharma',Australia_players:'Glen Maxwell',Pakistan_players:'Babar Azam',Southafrica_players:'Imran Tahir',England_players:'Alex Hales'},
        {Indian_players:'Jadeja',Australia_players:'Aron Finch',Pakistan_players:'Mohamad Hafeez',Southafrica_players:'David Miller',England_players:'James Anderson'},
        {Indian_players:'K.L.Rahul',Australia_players:'Mitchel Starc',Pakistan_players:'Imad Wasim',Southafrica_players:'Jp duminy',England_players:'Moeen Ali'},
        {Indian_players:'Bhuvaneswar Kumar',Australia_players:'Travis Head',Pakistan_players:'Shadab khan',Southafrica_players:'de kock',England_players:'Jos Buttler'},
        {Indian_players:'Shikar Dhawan',Australia_players:'Pat cummins',Pakistan_players:'yasir shah',Southafrica_players:'Hashim Amla',England_players:'Ben Strokes'},
        {Indian_players:'RishabPanth',Australia_players:'Mitchel Marsh',Pakistan_players:'Imam-ul-haq',Southafrica_players:'chris morris',England_players:'Sam Billings'},
        {Indian_players:'Ashwin',Australia_players:'Peter siddle',Pakistan_players:'Faheem Ashraf',Southafrica_players:'Aiden markram',England_players:'Eoin Morgan'},
        {Indian_players:'Dinesh Karthik',Australia_players:'Tim Paine',Pakistan_players:'Shoib Malik',Southafrica_players:'Dean Elgar',England_players:'chris Woakes'},

【问题讨论】:

  • 第一行表格你的意思是表头吗?
  • 请注意,您提供的 HTML 结构无效,您的表格正文中缺少一个打开的 &lt;tr&gt; 标记。我也怀疑你在tbody 上使用*ngFor 而不是tr
  • 不,我不明白
  • 我也添加了那个 标签

标签: html css angular7


【解决方案1】:

你可以用 CSS 做到这一点

custom-class:first-child { 
   color: yellow;
   font-size: 15px;
}

另外,您的示例中有一个错误,一个结束 tr 标记,但正文中没有开始 tr 标记

<tbody  *ngFor="let T of Teams" >
       <td> {{T.Indian_players}}</td>
       <td>{{T.Australia_players}}</td>
     </tr>
</tbody>

我相信在阅读完 cmets 之后,您可以/应该这样做,并在顶部使用 CSS,这一切都应该有效,我建议使用 class 而不是 tr 的原因是,如果您使用 tr,您已经使用了 tr 更多然后一次,它会影响所有 tr 标签,而不仅仅是你想要的那个。

 <tbody>
    <tr *ngFor="let T of Teams" class="custom-class">
        <td> {{T.Indian_players}}</td>
        <td>{{T.Australia_players}}</td>
    </tr>
</tbody>

【讨论】:

  • 不,它对我不起作用,但球员的名字没有改变。实际上我没有为队长写任何逻辑,我只是​​在名字后面加上(c)。所以有什么办法可以解决索引
  • 是的,如果你这样做 *ngfor="let T of Teams; let i = index" 然后你可以在块内的任何地方使用 i 作为当前索引
【解决方案2】:

您可以执行以下操作。

<tbody>
    <tr *ngFor="let T of Teams; let i = index" [class.your-captain-css-class]="index===0">
        <td> {{T.Indian_players}}</td>
        <td>{{T.Australia_players}}</td>
    </tr>
</tbody>

我做了以下事情。

  1. 获取索引和对象。
  2. 如果索引为0,则添加类。

这是满足您的要求。还有另一种只能通过 CSS 实现的方法。

【讨论】:

  • 你能用 CSS 来做吗
  • @geethaareti 我认为通过这种方法添加类比通过 CSS 更有效。不鼓励在 CSS 中使用 nth-of 运算符。
  • 好的,但我没有得到我想要的我已经在我的 中添加了相同的类,但我无法得到
  • 如何为该索引添加样式
猜你喜欢
相关资源
最近更新 更多
热门标签