2012-04-24 16 views
2

我有一個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,並且onclick div頁面應該重定向到一個位置。但是在這個div裏面也有一個錨標籤,但是點擊這個錨標籤頁應該重定向到另一個位置。有沒有可能在HTML或通過使用JavaScript來實現這一點。

回答

3

你可以試試這個。

$('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"; 
    } 
}); 
+0

這對我的作品。 – user359187 2012-04-25 18:33:37

0

這在IE 9,火狐11,和谷歌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> 
+0

我需要在ie7開始工作。 – user359187 2012-04-25 05:55:50

相關問題