是否有一个
Javascript包,通过单击它们并使用Shift和Ctrl键使表行可选/可突出显示?
我正在寻找与iTunes或其他音乐播放器相同的功能,它允许通过单击行突出显示一首歌曲或通过按住shift或控制并单击来突出显示多个.
解决方法
没有插件就可以做到!
此示例通过按住Ctrl,cmd(对于MacOS用户)或Shift键盘键并单击来突出显示一行或多行.
我建议避免使用插件来处理简单的事情.您将看到,实现您需要实现的代码并不是很多代码.
现场演示:http://jsfiddle.net/oscarj24/ctLm8/2/
HTML:
假设我有下表(您将与行交互).
<table border="1"> <tr> <th>Id</th> <th>Name</th> <th>Age</th> <th>Salary</th> </tr> <tr> <td>1</td> <td>Luis</td> <td>28</td> <td>$100,000</td> </tr> <tr> <td>2</td> <td>Oscar</td> <td>29</td> <td>$90,000</td> </tr> <tr> <td>3</td> <td>Daniel</td> <td>18</td> <td>$50,000</td> </tr> </table>
CSS:
现在我将创建一些CSS以在导航表时使用默认光标(仅用于样式,可以删除它),另一个用于突出显示行.
tr { cursor: default; } .highlight { background: yellow; }
jQuery的:
$(function() { /* Get all rows from your 'table' but not the first one * that includes headers. */ var rows = $('tr').not(':first'); /* Create 'click' event handler for rows */ rows.on('click',function(e) { /* Get current row */ var row = $(this); /* Check if 'Ctrl','cmd' or 'Shift' keyboard key was pressed * 'Ctrl' => is represented by 'e.ctrlKey' or 'e.MetaKey' * 'Shift' => is represented by 'e.shiftKey' */ if ((e.ctrlKey || e.MetaKey) || e.shiftKey) { /* If pressed highlight the other row that was clicked */ row.addClass('highlight'); } else { /* Otherwise just highlight one row and clean others */ rows.removeClass('highlight'); row.addClass('highlight'); } }); /* This 'event' is used just to avoid that the table text * gets selected (just for styling). * For example,when pressing 'Shift' keyboard key and clicking * (without this 'event') the text of the 'table' will be selected. * You can remove it if you want,I just tested this in * Chrome v30.0.1599.69 */ $(document).bind('selectstart dragstart',function(e) { e.preventDefault(); return false; }); });
最后,如果您坚持创建插件,可以查看此站点并根据需要自定义代码.