html表格行数代码,HTML表格可选行Javascript包

没有插件就可以做到!

此示例通过按住Ctrl,cmd(对于MacOS用户)或Shift键盘键并单击来突出显示一行或多行.

我建议避免使用插件来处理简单的事情.您将看到,实现您需要实现的代码并不是很多代码.

HTML:

假设我有下表(您将与行交互).

IdNameAgeSalary
1Luis28$100,000
2Oscar29$90,000
3Daniel18$50,000

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;

});

});

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值