html table row click,jquery - HTML table row link - Stack Overflow

Here is a jQuery plugin based on Nick's solution.

(function($) {

$.fn.linkWholeRows = function() {

// for each object

return this.each(function() {

// for each row

$(this).find('tbody tr').each(function() {

// get the first link's href

var href = $(this).find('td > a').attr('href');

// if none found then

if (href === undefined) {

return true; // continue

}

// wrap all cells with links that do not already have a link

$(this).children().not(':has(a)').each(function() {

$(this).contents().wrapAll('');

});

// apply the row's height to all links

// in case that the cells' content have different heights

var height = $(this).children().css('height');

$(this).find('td > a').each(function() {

$(this).css('height', height);

// do not forget to apply display:block to the links

// via css to make it work properly

});

}); // each row

}); // each object

};

})(jQuery);

Expects rows to be wrapped in tbody's. The height is set explicitly as Nick's original solution did not work for me on neighbouring cells with different heights.

Make sure to style a-elements as blocks. If you want to apply padding, apply it to the a-elements instead of table cells:

a {

display: block;

padding: 0.25em 0.5em;

}

tbody td { padding: 0; }

Simply call

$('#your-table').linkWholeRows();

Hope it helps. Cheers,

Richard

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值