Javascript div内的锚定标记。当div单击它时,页面应重定向到一个页面,如果锚定单击,页面应重定向到另一个页面

Javascript div内的锚定标记。当div单击它时,页面应重定向到一个页面,如果锚定单击,页面应重定向到另一个页面,javascript,jquery,html,Javascript,Jquery,Html,我有一个一div,里面有一个锚标签,像这样 <div onclick="page reddirec to one location" style="height:200px;width:200px"><a href="http://www.google.com" >text</a></div> 此处div的高度和宽度各为200px,单击div后,页面应重定向到一个位置。但是在这个div中也有一个锚定标记,但是一旦单击这个锚定标记,页面应该重

我有一个一div,里面有一个锚标签,像这样

<div onclick="page reddirec to one location"  style="height:200px;width:200px"><a href="http://www.google.com" >text</a></div>

此处div的高度和宽度各为200px,单击div后,页面应重定向到一个位置。但是在这个div中也有一个锚定标记,但是一旦单击这个锚定标记,页面应该重定向到另一个位置。是否可以在html或javascript中实现此功能。

您可以尝试此功能

$('div').click(function(e){
    if($(e.target).is('a')){
        //this will stop event bubbling and will open the href of the anchor
        e.stopPropagation();
    }
    else{
        location.href = "anotherUrl";
    }
});

这在IE 9、Firefox 11和Google Chrome 18中对我很有用:

<div onclick="window.location='http://www.yahoo.com'" style="height:200px;width:200px;background-color:Red;">
  <a href="http://www.google.com" >text</a>
</div>