2011-06-09 70 views
0

您好我有这样的HTML代码jQuery的鼠标按下找到该事件被解雇

<th scope="col" class="" style="width: 13px;"> 
    <div class="some-handle"></div> 
    <a href="javascript:__doPostBack('ucPWP$ctl03$3056$GVReport','Sort$3')">Passengers</a> 
</th> 

和详细的元素有这个JS代码

$("Table tr th").mousedown(function (e) { 
    /*mousedown code goes here... */ 
    /* ignore if mousedown occur on div "some-handle" */ 
    } 

现在,我尝试,如果鼠标按下发生在上课DIV赶上“一些手柄”。 我可以这样做吗?

回答

9

检查e.target。这是事件发生的元素。

if ($(e.target).hasClass('some-handle')) { 
    // fired on div "some-handle" 
} 
+0

我很抱歉 - 这不是回答你的问题。你问了当点击这个类的元素时如何防止事情发生。 – T9b 2011-06-09 08:01:02

+0

精彩的回答,不知道这个事件会不会对孩子元素起泡! – 2011-06-09 08:04:18

+0

@anasnakawa虽然你的评论对于代码是真实的,但这里OP所接受的答案根本不正确。当对象收到mousedown事件时,openidsujoy 说“忽略”。所以恕我直言,它不能解决问题。你甚至不能放一个'else',因为这会触发所有事情,这也是不正确的。 – T9b 2011-06-09 09:49:00

0

你可以做这样的事情

$("Table tr th").mousedown(function (e) { 

     if ($(this).hasClass('some-handle')) { 
     // ignore 
     return False; 
     } 
     /*mousedown code goes here... */ 
     } 
0

,你所要做的,是存储在th的值,当鼠标在你some-handle格,然后检查此值时鼠标在th元素:

$("Table tr th").mousedown(function (e) { 
    /*mousedown code goes here... */ 
    /* ignore if mousedown occur on div "some-handle" */ 
    if(!$(this).data('overSomeHandle')) { 
     // do your code here 
    } 
} 

$('.some-handle').hover(
    function(){ 
     // we are over some-handle div 
     $(this).parent().data('overSomeHandle', true); 
    }, 
    function(){ 
     // we are out of some-handle div 
     $(this).parent().data('overSomeHandle', false); 
    } 
); 
0

我觉得你有麻烦了,因为你有一个链接嵌套到您的<DIV>。点击链接会覆盖您怀疑的代码。

因此,你需要你实现代码之前处理click:

$("Table tr th").mousedown(function (e) 
{    
    e.preventDefault(); //this nullifies the click 
    if ($(this).hasClass('some-handle')) 
    {   
     // ignore   
     return False;   
    }else{ 
     /*mousedown code goes here... */   
    } 
});