简体   繁体   English

单击超链接时,jQuery将默认行的颜色传递给下一行

[英]JQuery Pass COLOR of default row to next row when hyperlink is clicked

hey guys its a zebra strip table for jquery im wondering how can i make the hyperlink onclick pass the color of certain row to the bottom or top? 大家好,它是jquery的斑马线表,我想知道如何让超链接onclick将某些行的颜色传递到底部或顶部? "Clicking the DN link must cause the highlighted row class .highlight to move up to the previous row. Loop the effect of the UP and DN buttons around the table so it doesn't stall when you reach the top or bottom of the table. " “单击DN链接必须使突出显示的行类.highlight移至上一行。在表格周围循环使用UP和DN按钮的效果,这样当您到达表格的顶部或底部时,它不会停止。 ”

my code 我的密码

 $(function(){ $(".odd").hover(function() { $(this).toggleClass("hovered"); }, function() { $(this).toggleClass("hovered"); }); }); $(function(){ $(".even").hover(function() { $(this).toggleClass("hovered"); }, function() { $(this).toggleClass("hovered"); }); }); 
  odd.hovered { background-color:red; } .even.hovered{ background-color:red; } .highlight{ background-color:yellow; } .odd{ background-color:grey; } .even{ background-color:#D3D3D3; } 
 <!-- saved from url=(0061)https://csunix.mohawkcollege.ca/tooltime/10065/f15/zebra.html --> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body> <h2>2: Zebra Striping Demo</h2> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> <table width="200" border="1"> <caption><a href="https://csunix.mohawkcollege.ca/tooltime/10065/f15/zebra.html#">UP</a> Zebra Striping Demo <a href="https://csunix.mohawkcollege.ca/tooltime/10065/f15/zebra.html#">DN</a></caption> <tbody><tr class="odd"> <td>January</td> <td>February</td> <td>March</td> </tr> <tr class="even"> <td>April</td> <td>May</td> <td>June</td> </tr> <tr class="highlight" > <td>July</td> <td>August</td> <td>September</td> </tr> <tr class="even"> <td>October</td> <td>November</td> <td>December</td> </tr> <tr class="odd"> <td>Monday</td> <td>Tuesday</td> <td> Wednesday</td> </tr> <tr class="even"> <td>Thursday</td> <td>Friday</td> <td>Saturday</td> </tr> <tr class="odd"> <td>Spring</td> <td>Summer</td> <td>Fall</td></tr> </tbody></table> </body></html> 

How about this? 这个怎么样?

 $(function() { $('#up').click(function(e) { e.preventDefault(); var $current = $('tr.highlight'), $previous = $current.prev(); // remove current row's highlight $current.removeClass('highlight'); // highlight previous row if it exist if ($previous.length) $previous.addClass('highlight'); // otherwise highlight last row else $current.siblings().last().addClass('highlight'); }); $('#down').click(function(e) { e.preventDefault(); var $current = $('tr.highlight'), $next = $current.next(); // remove current row's highlight $current.removeClass('highlight'); // highlight next row if it exist if ($next.length) $next.addClass('highlight'); // otherwise highlight last row else $current.siblings().first().addClass('highlight'); }); }); 
 tr.odd { background-color: grey; } tr.even { background-color: #D3D3D3; } tr.highlight { background-color: yellow; } tr:hover { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table width="200" border="1"> <caption><a href="https://csunix.mohawkcollege.ca/tooltime/10065/f15/zebra.html#" id="up">UP</a> Zebra Striping Demo <a href="https://csunix.mohawkcollege.ca/tooltime/10065/f15/zebra.html#" id="down">DN</a> </caption> <tbody> <tr class="odd"> <td>January</td> <td>February</td> <td>March</td> </tr> <tr class="even"> <td>April</td> <td>May</td> <td>June</td> </tr> <tr class="highlight"> <td>July</td> <td>August</td> <td>September</td> </tr> <tr class="even"> <td>October</td> <td>November</td> <td>December</td> </tr> <tr class="odd"> <td>Monday</td> <td>Tuesday</td> <td>Wednesday</td> </tr> <tr class="even"> <td>Thursday</td> <td>Friday</td> <td>Saturday</td> </tr> <tr class="odd"> <td>Spring</td> <td>Summer</td> <td>Fall</td> </tr> </tbody> </table> 

By the way, you don't need to add a class to each row. 顺便说一句,您不需要向每行添加一个类。 You could use the :nth-type selector in your CSS to create alternative styles. 您可以在CSS中使用:nth-​​type选择器来创建其他样式。

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

相关问题 JQuery表行.on(“点击”查找是否单击了超链接或只是行 - JQuery Table row .on(“click” find if a hyperlink was clicked or just row 如果单击超链接,如何从jqgrid行将数据传递到url - How to pass data to url from jqgrid row if hyperlink is clicked 在jQuery中单击时显示一行 - Display a row when clicked in jQuery 使用 jQuery 更改单击表格行的颜色 - Changing the color of a clicked table row using jQuery Javascript select 带有复选框的表格的整行(更改背景颜色)并在单击下一个复选框时取消选择 - Javascript select entire row (change background color) of a table with checkbox and deselect when next checkbox is clicked 单击TD时更改表的行颜色 - Change Row Color of Table When TD Is Clicked jQuery-单击下一个元素时向上滑动…仅在另一行上时 - Jquery - Slide up when next element is clicked…when only on a different row 在 jQuery 中单击两次时删除行正在工作 - Delete row is working when clicked twice in jQuery jQuery Datatables-如何在单击该行中的按钮时获取该行的索引 - jQuery Datatables - How to get the index of a row when a button in that row is clicked 如何在单击时更改表行颜色并返回到另一行单击时的原始行颜色? - How to change a table row color when clicked and back to what it was originally when another row clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM