Unfortunately there is no way to simulate a link and all associated behaviour in every browser. Therefore, the only way to achieve what you want is to have a link that follows the cursor around the <tr>
element; this link would be invisible so, to the user, it looks like they're clicking on the <tr>
but they're actually clicking on a hidden link. Using this method, the middle-button, ctrl+click and any other behaviours are left intact!
Here's a DEMO: http://jsbin.com/ufugo
And here's the code:
$("table tr").each(function(){
var $link = $('a:first', this).clone(true),
dim = {
x: [
$(this).offset().left,
$(this).offset().left + $(this).outerWidth()
],
y: [
$(this).offset().top,
$(this).offset().top + $(this).outerHeight()
]
}
$link
.click(function(){
$(this).blur();
})
.css({
position: 'absolute',
display: 'none',
// Opacity:0 means it's invisible
opacity: 0
})
.appendTo('body');
$(this).mouseover(function(){
$link.show();
});
$(document).mousemove(function(e){
var y = e.pageY,
x = e.pageX;
// Check to see if cursor is outside of <tr>
// If it is then hide the cloned link (display:none;)
if (x < dim.x[0] || x > dim.x[1] || y < dim.y[0] || y > dim.y[1]) {
return $link.hide();
}
$link.css({
top: e.pageY - 5,
left: e.pageX - 5
})
});
});
EDIT:
I created a jQuery plugin using a slightly better aproach than above: http://james.padolsey.com/javascript/table-rows-as-clickable-anchors/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…