3

我有一个基本表,如下所示:

<table>
    <tbody>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
        </tr>
    </tbody>
</table>

在行上,我有一个带有 jQ​​uery 的双击功能:

$('tr').live('dblclick',function(){
    dosomething();
    return false;
});

我还有一个名为 tablednd 的插件,它在行上使用 mousedown/up 函数。双击会导致单元格上的文本选择。

我怎样才能防止这种情况?

4

2 回答 2

11

您不能使用select()事件,因为它仅限于输入元素。

相反,尝试preventDefault()参加selectstart活动...

$('tr').bind('selectstart', function(event) {
    event.preventDefault();
});

js小提琴

或者,您可以使用 CSS ...

tr {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
于 2011-04-10T11:16:38.600 回答
0

尝试改变

$('tr').live('dblclick',function(){
    dosomething();
    return false;
});

$('tr').live('dblclick',function(e){
    e.preventDefault();
    dosomething();
    return false;
});

这应该可以防止双击执行浏览器默认执行的任何操作。不过,您应该在所有浏览器中测试此代码,但不确定它是否适用于所有地方。

于 2011-04-10T11:16:37.613 回答