简体   繁体   English

将可点击元素放置在可点击容器元素内

[英]Placing a clickable element inside a clickable container element

I'm trying to make the #header div clickable by wrapping a link element around it, but I cannot do it when it already has another image link inside the div. 我正在尝试通过在其周围包装链接元素来使#header div可单击,但是当div中已经具有另一个图像链接时,我将无法执行此操作。 How would I fix this? 我该如何解决?

 #header { border: 1px solid red; background-color: red; } img { width: 50px; height: 50px; } 
 <a href = 'index.php'> <div id = 'header'> <a href = 'profile.php?username=$username'> <img src = 'https://www.iscattered.com/uploads/1590Chocolate_chip_cookies.jpg'> </a> </div> </a> 

Now while the image link works just fine, the #wrapper div is not clickable. 现在,虽然图像链接可以正常工作,但#wrapper div不可单击。

 #header { border: 1px solid red; background-color: red; position: relative; /* establish nearest positioned ancestor for abs. positioning */ height: 50px; } #header a:first-child { display: block; height: 100%; } #header a:last-child { position: absolute; /* image now independently clickable */ top: 0; /* position image anywhere you want inside #header */ left: 0; } img { width: 50px; height: 50px; } 
 <div id='header'> <a href='index.php'></a> <a href='profile.php?username=$username'> <img src='https://www.iscattered.com/uploads/1590Chocolate_chip_cookies.jpg'> </a> </div> 

NOTES: 笔记:

  • If you wrap a hyperlink inside another hyperlink, how is the browser supposed to know which link to execute? 如果将超链接包装在另一个超链接中,浏览器应该如何知道要执行哪个链接?

  • Instead, make the #header element entirely clickable, and absolutely position the image. 而是使#header元素完全可单击,并完全定位图像。

  • Now the image can be clicked separately and positioned anywhere inside the #header element. 现在,可以单独单击图像并将其放置在#header元素内的任何位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM