如何防止在javascript中双击选择文本 [英] How to prevent the double-click select text in javascript

查看:86
本文介绍了如何防止在javascript中双击选择文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我在页面中有一个 ul li )列表:

Say I have an ul (li) list in the page:

<ul>
 <li>xxx<li>
 <li>xxx<li>
</ul>

元素 li 可点击并加倍可点击,它们附有这些事件,我在这两个事件中返回false

The element li are clickable and double-clickable, they are attached with these events, and I return false in both of them.

$('ul li').on('click',function(){
    //do what I want
    return false;
}).on('dblclick',function(){
    //do what I want
    return false;
});

但是当用户双击该元素时, li中的文本将被选中。如何防止这种情况?

But when the user double-clicks the element, the text inside the li will be selected. How can this be prevented?


更新:

现在解决了,我通过 NiftyDude 使用以下代码和css选择器:

Solved now,I use the following code with the css selector by NiftyDude:

$('ul li').on('click',function(){
    //do what I want
    return false;
}).....on('dragstart',function(){return false;}).on('selectstart',function(){return false;});


推荐答案

您可以使用css禁用文本选择(注意这个将有效禁用所有选择方法,而不仅仅是双击)

You can disable text selection using css (Note that this will effectively disable all selection methods and not just double clicking)

ul li {
   -webkit-touch-callout: none;
   -webkit-user-select: none;
   -khtml-user-select: none;
   -moz-user-select: none;
   -ms-user-select: none;
   user-select: none;
}

http://jsfiddle.net/T3d7v/1/

这篇关于如何防止在javascript中双击选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆