简体   繁体   English

获取列位置(如第一、第二等)

[英]get column position (like first, second, etc.)

when I click on an icon, it will change but I want to know in which column is clicked icon (like the first one or the second one or the third one, etc.)当我单击一个图标时,它会发生变化,但我想知道在哪一列中单击了图标(例如第一个或第二个或第三个等)

 $(".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 } });
 <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>

jQuery index function will return the index of the element. jQuery 索引函数将返回元素的索引。

 $(".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"); } });
 <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>

simply use event delegation and classList.replace() method只需使用事件委托和classList.replace()方法
(and it is more clear with css) (用css更清楚)

Each TD or TH element have a cellIndex property每个TDTH元素都有一个cellIndex属性

 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') }
 table { width: 100%; } table thead th i.fas { cursor : pointer; }
 <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>

but IMHO it would be cooler if the whole column header is used to invert the graph但恕我直言,如果使用整个列标题来反转图形会更酷

 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') }
 table { width: 100%; } table thead th { cursor : pointer; } table thead th:hover { background-color: lightgoldenrodyellow; } table thead th:hover i.fas { color: crimson; }
 <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>

You can use plain javascript to get it, let me show below:你可以使用普通的javascript来获取它,让我在下面展示:

$(".fa-caret-down").click(function(event){
    console.log(event.target.parentElement.cellIndex)
    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");
    }
});
  1. Pass the event as parameter into click function:将事件作为参数传递给点击函数:
  2. Using this element get the current element using target , aftet get its parent node using parentElement because the event is triggered clicking on <i><i/> element and it's inside a th , and finally get the cell index using cellIndex使用此元素使用target获取当前元素,后使用parentElement获取其父节点,因为该事件是在点击<i><i/>元素时触发的,并且它位于th ,最后使用cellIndex获取单元cellIndex
  3. You don't need a console.log inside if and else, you need only when the event id triggered if 和 else 中不需要 console.log,只有在事件 id 触发时才需要

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得第二行(第三行等)的缩进,以使用CSS使用自定义编号匹配列表中的第一行? - How can I get second (third, etc.) line indentation to match first line in a list with custom numbering using CSS? WordPress-如何按字母顺序排列jQuery AutoComplete搜索建议结果,优先考虑第一个字符,然后第二个等等。 - WordPress - How to alphabetically order jQuery AutoComplete search suggestion results, prioritizing first character, then second etc.? 维护文本等新结构js对象的对象大小和位置(根据通用画布) - Maintaining object size and position for new fabric js objects like text,etc., (acoording to general canvas) 如何从第二个 Javascript 文件中获取标签元素(标题、src 等) - How to get Tag Elements (Title, src etc.) from a second Javascript File 返回一个数组,该数组包含:一个第一项,两个第二项,树第三项等,使用循环 - Returns an array that consists of: one first item, two second items, tree third items etc. withour using loops ExtJS中列系列等的可靠示例? - A Solid Example of a Column Series, etc., in ExtJS? 获得班级宽度/高度等。 - Get a class width/height/etc.? 量角器-是否可以将$$(elementLocator).first()用于更多元素,例如第二,第三等等。 - Protractor - Can I use $$(elementLocator).first() for more elements like the second, third and etc..? 如何使用uId在Facebook中登录用户的信息,例如电子邮件ID,名称,城市等 - How I can get logged in user's info like email id,name,city etc. in Facebook with uId jQuery变量中有趣的html故障,其中包含&,/等字符? - Interesting html glitch in jquery variable with characters like &, /, etc.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM