javascript jQuery .click() 在选择/突出显示文本时触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10390010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery .click() is triggering when selecting/highlighting text
提问by krische
I have a <div>
with a bunch of text in it. This <div>
also has a .click()
event on it using jQuery.
我有<div>
一堆文字。这<div>
也有一个.click()
使用 jQuery的事件。
The problem I'm having is that the .click()
is being triggered when selecting/highlighting text. Even holding the mouse down for several seconds before releasing.
我遇到的问题.click()
是在选择/突出显示文本时被触发。甚至在释放之前按住鼠标几秒钟。
Here is a JSFiddle that shows the issue: http://jsfiddle.net/ym5JX/
这是一个显示问题的 JSFiddle:http: //jsfiddle.net/ym5JX/
The behavior that I would expect is that highlighting text isn't the same as clicking on the element.
我期望的行为是突出显示文本与单击元素不同。
回答by Rocket Hazmat
That's because a click
is a mousedown
followed by a mouseup
. My suggestion is to check getSelection
inside the click
handler. If it's set, then you selected something, else you just clicked.
那是因为 aclick
是 amousedown
后跟 a mouseup
。我的建议是检查处理程序getSelection
内部click
。如果已设置,则您选择了某些内容,否则您只需单击即可。
$('#click').click(function() {
var sel = getSelection().toString();
if(!sel){
alert("clicked");
}
});?
回答by Selvakumar Arumugam
As I posted on comment, mosuedown + mouseup = click
which is exactly what highlighting does. There is workaround for this.. see below,
正如我在评论中发布的那样,mosuedown + mouseup = click
这正是突出显示的作用。对此有解决方法..见下文,
var isClick = 0;
$('#click').click(function() {
if (isClick == 1) {
alert("clicked");
}
}).mousedown(function () {
isClick = 1;
}).mousemove(function () {
isClick = 0;
});
回答by Jonathan Payne
jsFiddle: http://jsfiddle.net/ym5JX/8/
jsFiddle:http: //jsfiddle.net/ym5JX/8/
$('#click').click( function()
{
if ( getSelection() == "" )
{
alert("clicked");
}
});