2016-11-29 103 views
3

我有div2(height 20px vertical align centre)裏面div1(100px)和div2有錨標記。Div懸停指針

懸停的div1我正在顯示手形指針,但也想在點擊任何地方點擊div1區域時,它應該導航到錨點標記網址。更多的細節

.test:hover { 
 
    cursor: pointer; 
 
    cursor: hand; 
 
}
<div class="test" style="height:100px"> 
 
    <div style=" position: relative; top: 50%; transform: translateY(-50%);"> 
 
    <a href="google.com" ; target="_blank" title="This link opens in a new window" style="color:#2e3940;text-decoration: initial;">Pawan Kotak</a> 
 
    </div> 
 
</div>

enter image description here

+4

請發表你的實際的HTML和CSS,連同你的代碼寫試圖解決這個自己的問題中 –

+0

<!DOCTYPE HTML> 頁面標題

+0

@RoryMcCrossan我已附加的代碼 –

回答

2

嘗試用以下方式

$(div1).on("click",function(){ 
    window.location = $(this).find("a").attr("href"); 
}); 

這僅僅是一個出路。

2

假設你的DIV ID的是div1div2這裏是一個解決方案,請參考圖片。

$('#div1').on('click',function(){ 
    $(this).find('a').trigger('click'); 
}); 

該腳本將附加click事件的div1和當過用戶點擊這個div它找到a標籤與它$(this).find('a'),並觸發對錨標記click事件。

+0

多次事件獲取一個爲div和一個錨標記當我點擊錨標記..如何我可以避免甚至 –

+0

@pawankotak多個觸發器你可以做'event.stopimmediatepropagation()'這將停止冒泡的事件.. –

+0

我試過不行 –

4

如果外「DIV」的行爲應該像一個錨,只是重新排序的元素是這樣的:

<a href="myAnchor.tld"> 
    <div id="div1"> 
    <div id="div2"></div> 
    </div> 
</a> 

您不必更動jQuery的,如果您的標記可以簡單地結構化的方式它真的有用。

1

你也可以增加你的錨標記(有幾種方法/辦法)

.test { 
 
    background-color: red; 
 
} 
 
.test a { 
 
    cursor: hand; 
 
    background-color: blue; 
 
    display: block; 
 
    line-height: 100px; 
 
}
<div class="test" style="height:100px"> 
 
    <div style=" position: relative; top: 50%; transform: translateY(-50%);"> 
 
    <a href="google.com" ; target="_blank" title="This link opens in a new window" style="color:#2e3940;text-decoration: initial;">Pawan Kotak</a> 
 
    </div> 
 
</div>

+0

我試過你的解決方案,但在我的情況下,一些多行文本也出現在創建問題的錨標記中。謝謝 –