微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

HTML表格可选行Javascript包

是否有一个 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; 
    });

});

最后,如果您坚持创建插件,可以查看此站点并根据需要自定义代码.

http://learn.jquery.com/plugins/basic-plugin-creation/

其他功能只取决于你,我刚刚回答了你在问题中所要求的内容:-)希望这会有所帮助.

原文地址:https://www.jb51.cc/html/224901.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐