2011-10-11 42 views

回答

4

我不知道你想要什麼,但加入 -

$('a').click(function(e) { 
     e.stopPropagation(); 
     e.preventDefault(); 
    }); 

將阻止鏈接點擊從發射,也停止鏈接點擊冒泡到'div'點擊事件。

演示 - http://jsfiddle.net/ipr101/Lf3hL/

+0

注意,這是相當於'返回假'在[我的答案](http://stackoverflow.com/questions/7729372/jquery-div-with-link-inside-and-jq-click-function/7729540#7729540)。 – Eric

0

是的,你可以使用

$('#test').click(function(event) { 
     event.preventDefault(); 
}); 

event.preventDefault(),使點擊不行

0

你必須停止對a click事件冒泡達div#test。你可以是這樣做的:

$(document).ready(function() { 
    $('#test').click(function() { 
     alert('the link was not clicked'); 
    }); 
    $('#test a').click(function(e) { 
     return false; 

     //equivalent to: 
     // e.stopPropagation(); 
     // e.preventDefault(); 
    }); 
}); 

demo

0

可以與事件工作或簡單地返回false:

http://jsbin.com/aliwih/edit#source

$(document).ready(function() { 
    $('#test').click(function() { 
     $("#hello").text('the link was not clicked'); 
     return false; 
    }); 
    $('#test a').click(function() { 
    $("#hello").text('the link clicked'); 
    return false; 
    }); 
});