首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获得列的位置(如第一、第二等)

获得列的位置(如第一、第二等)
EN

Stack Overflow用户
提问于 2021-11-09 15:05:55
回答 2查看 69关注 0票数 0

当我单击一个图标时,它会改变,但我想知道在哪个列中单击了图标(比如第一个或第二个或第三个)。

代码语言:javascript
复制
$(".fa-caret-down").click(function(){
    if($(this).is(".fa-caret-down")){
        $(this).removeClass("fa-caret-down").addClass("fa-caret-up");
        //console.log() - in which column it was changed
    }else{
        $(this).removeClass("fa-caret-up").addClass("fa-caret-down");
        //console.log() - in which column it was changed
    }
});
代码语言:javascript
复制
<table style="width:100%">
    <thead>
        <tr>
            <th>#<i class="fas fa-caret-down ml-2" style="cursor: pointer;"></i></th>
            <th>Name<i class="fas fa-caret-down ml-2" style="cursor: pointer;"></i></th>
            <th>Price<i class="fas fa-caret-down ml-2" style="cursor: pointer;"></i></th>
            <th>1h %<i class="fas fa-caret-down ml-2" style="cursor: pointer;"></i></th>
            <th>24h %<i class="fas fa-caret-down ml-2" style="cursor: pointer;"></i></th>
            <th>Timestamp<i class="fas fa-caret-down ml-2 align" style="cursor: pointer;"></i></th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-09 15:20:45

jQuery索引函数将返回元素的索引。

代码语言:javascript
复制
$(".fa-caret-down").click(function(){
    console.log($(this).parent().index()) //- in which column it was changed
    if($(this).is(".fa-caret-down")){
        $(this).removeClass("fa-caret-down").addClass("fa-caret-up");
    }else{
        $(this).removeClass("fa-caret-up").addClass("fa-caret-down");
    }
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<table style="width:100%">
    <thead>
        <tr>
            <th>#<i class="fas fa-caret-down ml-2" style="cursor: pointer;"></i></th>
            <th>Name<i class="fas fa-caret-down ml-2" style="cursor: pointer;"></i></th>
            <th>Price<i class="fas fa-caret-down ml-2" style="cursor: pointer;"></i></th>
            <th>1h %<i class="fas fa-caret-down ml-2" style="cursor: pointer;"></i></th>
            <th>24h %<i class="fas fa-caret-down ml-2" style="cursor: pointer;"></i></th>
            <th>Timestamp<i class="fas fa-caret-down ml-2 align" style="cursor: pointer;"></i></th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

票数 2
EN

Stack Overflow用户

发布于 2021-11-09 15:28:01

只需使用事件委托和classList.replace()方法

(css更清楚)

每个TDTH元素都有一个cellIndex属性

代码语言:javascript
复制
document.querySelector('table thead').onclick = e =>
  {
  if (!e.target.matches('i.fas')) return   // ignore clicks from elsewhere

  let eTH    = e.target.closest('th')
    , onDown = e.target.classList.contains('fa-caret-down')
    ;
  console.clear()
  console.log( 'column :', eTH.cellIndex, eTH.innerText, onDown?'UP':'DOWN'  ),
  setTimeout(console.clear,3000)

  if (onDown) e.target.classList.replace('fa-caret-down','fa-caret-up')
  else        e.target.classList.replace('fa-caret-up','fa-caret-down')
  }
代码语言:javascript
复制
table {
  width: 100%;
}
table thead th i.fas {
  cursor : pointer;
}
代码语言:javascript
复制
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css" >

<table >
  <thead>
    <tr>
      <th>#<i class="fas fa-caret-down ml-2" ></i></th>
      <th>Name<i class="fas fa-caret-down ml-2" ></i></th>
      <th>Price<i class="fas fa-caret-down ml-2" ></i></th>
      <th>1h %<i class="fas fa-caret-down ml-2" ></i></th>
      <th>24h %<i class="fas fa-caret-down ml-2" ></i></th>
      <th>Timestamp<i class="fas fa-caret-down ml-2 align" ></i></th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

但是IMHO如果使用整个列标题来反转图,会更酷。

代码语言:javascript
复制
document.querySelector('table thead').onclick = ({target}) =>
  {
  if (!target.matches('table thead th, table thead th i.fas')) return  // ignore clicks from elsewhere

  let eIfas  = target.querySelector('i.fas')
    , eTH    = target.closest('th')
    , onDown = eIfas.classList.contains('fa-caret-down')
    ;
  console.clear()
  console.log( 'column :', eTH.cellIndex, ':',eTH.innerText, onDown?'UP':'DOWN'  )
  setTimeout(console.clear,5000)

  if (onDown)  eIfas.classList.replace('fa-caret-down','fa-caret-up')
  else         eIfas.classList.replace('fa-caret-up','fa-caret-down')
  }
代码语言:javascript
复制
table {
  width: 100%;
}
table thead th {
  cursor : pointer;
}
table thead th:hover {
  background-color: lightgoldenrodyellow;
}
table thead th:hover i.fas {
  color: crimson;
}
代码语言:javascript
复制
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css" >

<table >
  <thead>
    <tr>
      <th># <i class="fas fa-caret-down ml-2" ></i></th>
      <th>Name <i class="fas fa-caret-down ml-2" ></i></th>
      <th>Price <i class="fas fa-caret-down ml-2" ></i></th>
      <th>1h % <i class="fas fa-caret-down ml-2" ></i></th>
      <th>24h % <i class="fas fa-caret-down ml-2" ></i></th>
      <th>Timestamp <i class="fas fa-caret-down ml-2 align" ></i></th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69900485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档