有一种方法可以在JavaScript中实现这一目标.您可以使用跨度替换链接并处理mousedown,然后查看是否进行了选择,如果没有模拟链接单击,则可以.
码:
var posX = -1, posY = -1;
$('a').each(function() {
var a = $(this);
a.replaceWith(
$('')
.text(a.text())
.attr({
href: a.attr('href'),
target: a.attr('target')
})
.addClass('selectable')
);
});
$(document).on('mousedown', '.selectable', function(evt) {
posX = evt.pageX;
posY = evt.pageY;
});
$(document).on('mouseup', '.selectable', function(evt) {
if(evt.pageX == posX && evt.pageY == posY) {
alert('no selection. simulate click.');
window.location.href = $(this).attr('href');
// TODO: handle target attribute correctly
}
});