[英]How To Add An “a href” Link To A “div”?
我需要知道如何在div中添加一個href鏈接? 你把一個href標簽放在整個“buttonOne”div上嗎? 或者它應該在“linkedinB”div周圍或內部?
這是我的HTML:
<div id="buttonOne">
<div id="linkedinB">
<img src="img/linkedinB.png" width="40" height="40">
</div>
</div>
你不能用一個標簽包圍它嗎?
<a href="#"><div id="buttonOne">
<div id="linkedinB">
<img src="img/linkedinB.png" width="40" height="40">
</div>
</div></a>
在這種情況下,它並不重要,因為兩個div
之間沒有內容。
任何一個都會讓瀏覽器向下滾動到它。
a
元素看起來像:
<a href="mypageName.html#buttonOne">buttonOne</a>
要么:
<a href="mypageName.html#linkedinB">linkedinB</a>
嘗試用javascript實現這個:
<div id="mydiv" onclick="myhref('http://web.com');" >some stuff </div>
<script type="text/javascript">
function myhref(web){
window.location.href = web;}
</script>
嘗試創建一個名為overlay
的類,並將以下css應用於它:
a.overlay { width: 100%; height:100%; position: absolute; }
確保將其放置在定位元素中。
現在只需將一個<a>
標簽與該類放在您想要鏈接的div中:
<div id="buttonOne">
<a class="overlay" href="......."></a>
<div id="linkedinB">
<img src="img/linkedinB.png" alt="never forget the alt tag" width="40" height="40"/>
</div>
</div>
PhilipK的建議可能有效,但它不會驗證,因為你不能在一個內聯元素( a
)中放置一個塊元素( div
)。 當你的網站沒有驗證W3C Ninja將會為你而來!
另一個建議是嘗試避免內聯樣式。
我會說:
<a href="#"id="buttonOne">
<div id="linkedinB">
<img src="img/linkedinB.png" width="40" height="40">
</div>
</div>
但是,它仍然是一個鏈接。 如果您想將鏈接更改為按鈕,則應將#buttonone重命名為#buttonone a {your css here}。
您的解決方案似乎不適合我,我有以下代碼。 如何將鏈接放入最后兩個div。
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<style>
/* Import */
@import url(https://fonts.googleapis.com/css?family=Quicksand:300,400);
* {
font-family: "Quicksand", sans-serif;
font-weight: bold;
text-align: center;
text-transform: uppercase;
-webkit-transition: all 0.25s ease;
-moz-transition: all 0.25s ease;
-ms-transition: all 0.25s ease;
-o-transition: all 0.025s ease;
}
/* Colors */
#ora {
background-color: #e67e22;
}
#red {
background-color: #e74c3c;
}
#orab {
background-color: white;
border: 5px solid #e67e22;
}
#redb {
background-color: white;
border: 5px solid #e74c3c;
}
/* End of Colors */
.B {
width: 240px;
height: 55px;
margin: auto;
line-height: 45px;
display: inline-block;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-o-box-sizing: border-box;
}
#orab:hover {
background-color: #e67e22;
}
#redb:hover {
background-color: #e74c3c;
}
#whib:hover {
background-color: #ecf0f1;
}
/* End of Border
.invert:hover {
-webkit-filter: invert(1);
-moz-filter: invert(1);
-ms-filter: invert(1);
-o-filter: invert(1);
}
</style>
<h1>Flat and Modern Buttons</h1>
<h2>Border Stylin'</h2>
<div class="B bo" id="orab">See the movies list</div></a>
<div class="B bo" id="redb">Avail a free rental day</div>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.