将div变成链接

发布于 2024-07-18 06:57:45 字数 171 浏览 7 评论 0原文

我有一个

块,其中包含一些我不想更改的精美视觉内容。 我想让它成为一个可点击的链接。

我正在寻找类似

,但这是有效的 XHTML 1.1。

I have a <div> block with some fancy visual content that I don't want to change. I want to make it a clickable link.

I'm looking for something like <a href="…"><div> … </div></a>, but that is valid XHTML 1.1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

分开我的手 2024-07-25 06:57:45

来到这里是希望找到一个更好的解决方案,但我不喜欢这里提供的任何解决方案。 我认为你们中有些人误解了这个问题。 OP 想让一个充满内容的 div 表现得像一个链接。 Facebook 广告就是一个例子 - 如果你仔细观察,它们实际上是正确的标记。

对我来说,禁忌是: javascript(不应该仅仅为了链接而需要,而且搜索引擎优化/可访问性非常糟糕); 无效的 HTML。

本质上是这样的:

  • 使用正常的 CSS 技术和有效的 HTML 构建面板。
  • 在其中的某个位置放置一个链接,如果用户单击面板,您希望该链接成为默认链接(您也可以有其他链接)。
  • 在该链接内,放置一个空的 span 标签(,而不是 - 感谢 @Campey)
  • 给出面板位置:relative
  • 将以下 CSS 应用到空范围:

    <前><代码>{
    位置:绝对;
    宽度:100%;
    高度:100%;
    顶部:0;
    左:0;

    z 索引:1;

    /* 修复 IE7/8 中的重叠错误,
    确保你有一个空的 gif */
    背景图像: url('empty.gif');
    }

    它现在将覆盖面板,并且由于它位于 标记内,因此它是一个可点击的链接

  • 为面板内的任何其他链接提供位置:相对和合适的 z-index (>1) 以将它们放在前面默认跨度链接

Came here in the hope of finding a better solution that mine, but I don't like any of the ones on offer here. I think some of you have misunderstood the question. The OP wants to make a div full of content behave like a link. One example of this would be facebook ads - if you look, they're actually proper markup.

For me the no-nos are: javascript (shouldn't be needed just for a link, and very bad SEO/accessibility); invalid HTML.

In essence it's this:

  • Build your panel using normal CSS techniques and valid HTML.
  • Somewhere in there put a link that you want to be the default link if the user clicks on the panel (you can have other links too).
  • Inside that link, put an empty span tag (<span></span>, not <span /> - thanks @Campey)
  • give the panel position:relative
  • apply the following CSS to the empty span:

    { 
      position:absolute; 
      width:100%;
      height:100%;
      top:0;
      left: 0;
    
      z-index: 1;
    
      /* fixes overlap error in IE7/8, 
         make sure you have an empty gif */
      background-image: url('empty.gif');
    }   
    

    It will now cover the panel, and as it's inside an <A> tag, it's a clickable link

  • give any other links inside the panel position:relative and a suitable z-index (>1) to bring them in front of the default span link
还不是爱你 2024-07-25 06:57:45

您不能使 div 本身成为链接,但可以使 标记充当 block,行为相同

有。

a {
    display: block;
}

然后您可以设置其宽度和高度。

You can't make the div a link itself, but you can make an <a> tag act as a block, the same behaviour a <div> has.

a {
    display: block;
}

You can then set the width and height on it.

孤独患者 2024-07-25 06:57:45

这是一个古老的问题,但我想我会回答它,因为这里的每个人都有一些疯狂的解决方案。 它实际上非常非常简单...

锚标记的工作原理是这样的 -

<a href="whatever you want"> EVERYTHING IN HERE TURNS INTO A LINK </a>

Sooo...

<a href="whatever you want"> <div id="thediv" /> </a>

虽然我不确定这是否有效。 如果这就是口头解决方案背后的原因,那么我深表歉意......

This is an ancient question, but I thought I'd answer it since everyone here has some crazy solutions. It's actually very very simple...

An anchor tag works like this -

<a href="whatever you want"> EVERYTHING IN HERE TURNS INTO A LINK </a>

Sooo...

<a href="whatever you want"> <div id="thediv" /> </a>

Although I'm not sure if this is valid. If that's the reasoning behind spoken solutions, then I apologise...

野侃 2024-07-25 06:57:45

需要一点 JavaScript。
但是,您的 div 将是可点击的。

<div onclick="location.href='http://www.example.com';" style="cursor:pointer;"></div>

Requires a little javascript.
But, your div would be clickable.

<div onclick="location.href='http://www.example.com';" style="cursor:pointer;"></div>
空袭的梦i 2024-07-25 06:57:45

此选项不需要空.gif,如最受支持的答案中所示:

HTML:

 <div class="feature">
       <a href="http://www.example.com"></a>
 </div>

CSS:

 div.feature {
        position: relative;
    }

    div.feature a {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        text-decoration: none; /* No underlines on the link */
        z-index: 10; /* Places the link above everything else in the div */
        background-color: #FFF; /* Fix to make div clickable in IE */
        opacity: 0; /* Fix to make div clickable in IE */
        filter: alpha(opacity=1); /* Fix to make div clickable in IE */
    }

按照 http://www.digitalskydesign.com/how-to-make-an-entire-div-a-link-using-css/

This option doesn’t require an empty.gif as in the most upvoted answer:

HTML:

 <div class="feature">
       <a href="http://www.example.com"></a>
 </div>

CSS:

 div.feature {
        position: relative;
    }

    div.feature a {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        text-decoration: none; /* No underlines on the link */
        z-index: 10; /* Places the link above everything else in the div */
        background-color: #FFF; /* Fix to make div clickable in IE */
        opacity: 0; /* Fix to make div clickable in IE */
        filter: alpha(opacity=1); /* Fix to make div clickable in IE */
    }

As proposed at http://www.digitalskydesign.com/how-to-make-an-entire-div-a-link-using-css/

‖放下 2024-07-25 06:57:45

最干净的方法是使用 jQuery 和 HTML 中引入的数据标签。 使用此解决方案,您可以在您想要的每个标签上创建链接。 首先使用数据链接标签定义标签(例如div):

<div data-link="http://www.google.at/">Some content in the div which is arbitrary</div>

现在您可以根据需要设置div 的样式。 并且您还必须为“链接”类似行为创建样式:

[data-link] {
  cursor: pointer;
}

最后将此 jQuery 调用放入页面:

$(document).ready(function() {
  $("[data-link]").click(function() {
    window.location.href = $(this).attr("data-link");
    return false;
  });
});

使用此代码,jQuery 将单击侦听器应用于页面上具有“数据链接”的每个标记属性并重定向到 data-link 属性中的 URL。

The cleanest way would be to use jQuery with the data-tags introduced in HTML. With this solution you can create a link on every tag you want. First define the tag (e.g. div) with a data-link tag:

<div data-link="http://www.google.at/">Some content in the div which is arbitrary</div>

Now you can style the div however you want. And you have to create also the style for the "link"-alike behavior:

[data-link] {
  cursor: pointer;
}

And at last put this jQuery call to the page:

$(document).ready(function() {
  $("[data-link]").click(function() {
    window.location.href = $(this).attr("data-link");
    return false;
  });
});

With this code jQuery applys a click listener to every tag on the page which has a "data-link" attribute and redirects to the URL which is in the data-link attribute.

岁月流歌 2024-07-25 06:57:45

这是实现您想要的目标的“有效”解决方案。

<style type="text/css">
.myspan {
    display: block;
}
</style>
<a href="#"><span class="myspan">text</span></a>

但很可能您真正想要的是将 标记显示为块级元素。

我不建议使用 JavaScript 来模拟超链接,因为这违背了标记验证的目的,而标记验证的最终目的是促进可访问性(遵循正确的语义规则发布格式良好的文档可以最大限度地减少不同浏览器对同一文档进行不同解释的可能性)。

最好发布一个未经验证的网页,但可以在所有浏览器上正常呈现和运行,包括禁用 JavaScript 的浏览器。 此外,使用 onclick 不会为屏幕阅读器提供语义信息来确定 div 是否充当链接。

This is a "valid" solution to achieving what you want.

<style type="text/css">
.myspan {
    display: block;
}
</style>
<a href="#"><span class="myspan">text</span></a>

But most-likely what you really want is to have an <a> tag displayed as a block level element.

I would not advise using JavaScript to simulate a hyperlink as that defeats the purpose of markup validation, which is ultimately to promote accessibility (publishing well-formed documents following proper semantic rules minimizes the possibility the same document will be interpreted differently by different browsers).

It would be preferable to publish a web page that does not validate, but renders and functions properly on all browsers, including ones with JavaScript disabled. Furthermore, using onclick does not provide the semantic information for a screen reader to determine that the div is functioning as a link.

小糖芽 2024-07-25 06:57:45

不确定这是否有效,但它对我有用。

代码 :

<div style='position:relative;background-color:#000000;width:600px;height:30px;border:solid;'>
  <p style='display:inline;color:#ffffff;float:left;'> Whatever </p>     
  <a style='position:absolute;top:0px;left:0px;width:100%;height:100%;display:inline;' href ='#'></a>
</div>

Not sure if this is valid but it worked for me.

The code :

<div style='position:relative;background-color:#000000;width:600px;height:30px;border:solid;'>
  <p style='display:inline;color:#ffffff;float:left;'> Whatever </p>     
  <a style='position:absolute;top:0px;left:0px;width:100%;height:100%;display:inline;' href ='#'></a>
</div>

焚却相思 2024-07-25 06:57:45

为了使同行的答案在 IE 7 及更高版本中工作,需要进行一些调整。

  1. 如果元素没有背景颜色,IE 将不支持 z-index,因此链接不会重叠包含内容的 div 的部分,只会重叠空白部分。 为了解决这个问题,添加了不透明度为 0 的背景。

  2. 出于某种原因,在链接方法中使用跨度时,IE7 和各种兼容模式完全失败。 然而,如果链接本身被赋予了样式,它就可以正常工作。

.blockLink  
{  
    position:absolute;  
    top:0;  
    left: 0;  
    width:100%;  
    height:100%;  
    z-index: 1;  
    background-color:#ffffff;   
    -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";  
    filter: alpha(opacity=0);  
    opacity:0;  
}
<div style="position:relative">  
    <some content>  
    <a href="somepage" class="blockLink" />  
<div>

To make thepeer's answer work in IE 7 and forward, it needs a few tweaks.

  1. IE will not honour z-index if the element is has no background-color, so the link will not overlap parts of the containig div that has content, only the blank parts. To fix this a background is added with opacity 0.

  2. For some reason IE7 and various compatibility modes completely fail when using the span in a link approach. However if the link itself is given the style it works just fine.

.blockLink  
{  
    position:absolute;  
    top:0;  
    left: 0;  
    width:100%;  
    height:100%;  
    z-index: 1;  
    background-color:#ffffff;   
    -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";  
    filter: alpha(opacity=0);  
    opacity:0;  
}
<div style="position:relative">  
    <some content>  
    <a href="somepage" class="blockLink" />  
<div>
归属感 2024-07-25 06:57:45

您还可以尝试包裹一个锚点,然后将其高度和宽度与其父级相同。 这对我来说非常有效。

<div id="css_ID">
    <a href="http://www.your_link.com" style="display:block; height:100%; width:100%;"></a>
</div>

you could also try by wrapping an anchor, then turning its height and width to be the same with its parent. This works for me perfectly.

<div id="css_ID">
    <a href="http://www.your_link.com" style="display:block; height:100%; width:100%;"></a>
</div>
甜是你 2024-07-25 06:57:45

没有提到的一个选项是使用 flex。 通过将 flex: 1 应用于 a 标记,它会扩展以适合容器。

div {
  height: 100px;
  width: 100px;
  display: flex;
  border: 1px solid;
}

a {
  flex: 1;
}
<div>
  <a href="http://google.co.uk">Link</a>
</div>

An option that hasn't been mentioned is using flex. By applying flex: 1 to the a tag, it expands to fit the container.

div {
  height: 100px;
  width: 100px;
  display: flex;
  border: 1px solid;
}

a {
  flex: 1;
}
<div>
  <a href="http://google.co.uk">Link</a>
</div>

彩扇题诗 2024-07-25 06:57:45

这对我有用:

HTML:

<div>

  WHATEVER YOU WANT

  <a href="YOUR LINK HERE">
    <span class="span-link"></span>
  </a>

</div>

CSS:

.span-link {
  position:absolute;
  width:100%;
  height:100%;
  top:0;
  left: 0;
  z-index: 9999;
}

这添加了一个不可见元素(跨度),它覆盖了整个 div,并且位于 z 索引上的整个 div 之上,因此当有人单击该元素时div,点击本质上是被链接的不可见的“span”层拦截的。

注意:如果您已经对其他元素使用 z 索引,只需确保此 z 索引的值高于您希望其“位于顶部”的任何值。

This worked for me:

HTML:

<div>

  WHATEVER YOU WANT

  <a href="YOUR LINK HERE">
    <span class="span-link"></span>
  </a>

</div>

CSS:

.span-link {
  position:absolute;
  width:100%;
  height:100%;
  top:0;
  left: 0;
  z-index: 9999;
}

This adds an invisible element (the span), which covers your entire div, and is above your whole div on the z-index, so when someone clicks on that div, the click is essentially intercepted by your invisible "span" layer, which is linked.

Note: If you're already using z-indexes for other elements, just make sure the value of this z-index is higher than anything you want it to rest "on top" of.

扛起拖把扫天下 2024-07-25 06:57:45

为什么不? 使用

在 HTML5 中工作正常

why not? use <a href="bla"> <div></div> </a> works fine in HTML5

暮光沉寂 2024-07-25 06:57:45

这个例子对我有用:

<div style="position: relative; width:191px; height:83px;">
    <a href="link.php" style="display:block; width:100%; height:100%;"></a>
</div>

This example worked for me:

<div style="position: relative; width:191px; height:83px;">
    <a href="link.php" style="display:block; width:100%; height:100%;"></a>
</div>
寒江雪… 2024-07-25 06:57:45

我知道这篇文章很旧,但我只需要解决同样的问题,因为简单地编写一个正常的链接标签并将显示设置为阻止并不会使整个 div 在 IE 中可点击。 因此解决这个问题比使用 JQuery 简单得多。

首先让我们了解为什么会发生这种情况:IE 不会使空 div 可点击,它只会使该 div/a 标签内的文本/图像可点击。

解决方案:用背景图像填充 div 并将其对查看者隐藏。

如何?
你问了好问题,现在听着。
将这个背景样式添加到 a 标签中

> "background:url('some_small_image_path')
> -2000px -2000px no-repeat;"

,这样整个 div 现在就可以点击了。 这对我来说是最好的方法,因为我将它用于我的照片库,让用户单击图像的一半向左/向右移动,然后放置一张小图像以实现视觉效果。 所以对我来说,无论如何我都使用左右图像作为背景图像!

This post is Old I know but I just had to fix the same issue because simply writing a normal link tag with the display set to block does not make the whole div clickable in IE. so to fix this issue far simpler than having to use JQuery.

Firstly let us understand why this happens: IE wont make an empty div clickable it only make the text/image within that div/a tag clickable.

Solution: Fill the div with a bakground image and hide it from the viewer.

How?
You ask good questions, now listen up.
add this backround style to the a tag

> "background:url('some_small_image_path')
> -2000px -2000px no-repeat;"

And there you have it the whole div is now clickable. This was the best way for me cause Im using it for my Photo Gallery to let the user clik on one half of the image to move left/right and then place a small image as well just for visual effects. so for me I used the left and right images as background images anyway!

雨后彩虹 2024-07-25 06:57:45

只需将链接放在块中并使用 jquery 对其进行增强即可。 对于任何没有 JavaScript 的人来说,它可以 100% 优雅地降级。 恕我直言,用 html 来做这件事并不是最好的解决方案。
例如:

<div id="div_link">
<h2><a href="mylink.htm">The Link and Headline</a></h2>
<p>Some more stuff and maybe another <a href="mylink.htm">link</a>.</p>
</div>

然后使用jquery使块可点击(通过网页设计师墙):

$(document).ready(function(){

    $("#div_link").click(function(){
      window.location=$(this).find("a").attr("href"); return false;
    });

});

那么您所要做的就是将光标样式添加到 div 中。

    #div_link:hover {cursor: pointer;}

为了获得奖励积分,仅当通过向 div、正文或其他内容添加“js_enabled”类来启用 javascript 时才应用这些样式。

Just have the link in the block and enhance it with jquery. It degrades 100% gracefully for anyone without javascript. Doing this with html isn't really the best solution imho.
For example:

<div id="div_link">
<h2><a href="mylink.htm">The Link and Headline</a></h2>
<p>Some more stuff and maybe another <a href="mylink.htm">link</a>.</p>
</div>

Then use jquery to make the block clickable (via web designer wall):

$(document).ready(function(){

    $("#div_link").click(function(){
      window.location=$(this).find("a").attr("href"); return false;
    });

});

Then all you have to do is add cursor styles to the div

    #div_link:hover {cursor: pointer;}

For bonus points only apply these styles if javascript is enabled by adding a 'js_enabled' class to the div, or the body, or whatever.

盗琴音 2024-07-25 06:57:45

这是 BBC 网站和卫报上使用的最佳方法:

我在这里找到了该技术:
http://codepen.io/IschaGast/pen/Qjxpxo

这里是html,

<div class="highlight block-link">
      <h2>I am an example header</h2>
      <p><a href="pageone" class="block-link__overlay-link">This entire box</a> links somewhere, thanks to faux block links. I am some example text with a <a href="pagetwo">custom link</a> that sits within the block</p>

</div>

这里是CSS

/**
 * Block Link
 *
 * A Faux block-level link. Used for when you need a block-level link with
 * clickable areas within it as directly nesting a tags breaks things.
 */


.block-link {
    position: relative;
}

.block-link a {
  position: relative;
  z-index: 1;
}

.block-link .block-link__overlay-link {
    position: static;
    &:before {
      bottom: 0;
      content: "";
      left: 0;
      overflow: hidden;
      position: absolute;
      right: 0;
      top: 0;
      white-space: nowrap;
      z-index: 0;
    }
    &:hover,
    &:focus {
      &:before {
        background: rgba(255,255,0, .2);
      }
    }
}

This is the best way to do it as used on the BBC website and the Guardian:

I found the technique here:
http://codepen.io/IschaGast/pen/Qjxpxo

heres the html

<div class="highlight block-link">
      <h2>I am an example header</h2>
      <p><a href="pageone" class="block-link__overlay-link">This entire box</a> links somewhere, thanks to faux block links. I am some example text with a <a href="pagetwo">custom link</a> that sits within the block</p>

</div>

heres the CSS

/**
 * Block Link
 *
 * A Faux block-level link. Used for when you need a block-level link with
 * clickable areas within it as directly nesting a tags breaks things.
 */


.block-link {
    position: relative;
}

.block-link a {
  position: relative;
  z-index: 1;
}

.block-link .block-link__overlay-link {
    position: static;
    &:before {
      bottom: 0;
      content: "";
      left: 0;
      overflow: hidden;
      position: absolute;
      right: 0;
      top: 0;
      white-space: nowrap;
      z-index: 0;
    }
    &:hover,
    &:focus {
      &:before {
        background: rgba(255,255,0, .2);
      }
    }
}
暖阳 2024-07-25 06:57:45

实际上你现在需要包含 JavaScript 代码,
请查看本教程来执行此操作。

但有一个棘手的方法可以使用 CSS 代码来实现这一点
您必须在 div 标签内嵌套一个锚标记,并且必须向其应用此属性,

display:block;

当您这样做时,如果您想覆盖,它将使整个宽度区域可点击(但在锚标记的高度内)整个 div 区域,您必须将锚标记的高度精确设置为 div 标记的高度,例如:

height:60px;

这将使整个区域可点击,然后您可以应用 text-indent:-9999px 锚定标签以实现目标。

这确实很棘手也很简单,而且它只是使用 CSS 代码创建的。

这是一个示例

Actually you need to include the JavaScript code at the moment,
check this tutorial to do so.

but there is a tricky way to achieve this using a CSS code
you must nest an anchor tag inside your div tag and you must apply this property to it,

display:block;

when you've done that,it will make the whole width area clickable (but within the height of the anchor tag),if you want to cover the whole div area you must set the height of the anchor tag exactly to the height of the div tag,for example:

height:60px;

this is gonna make the whole area clickable,then you can apply text-indent:-9999px to anchor tag to achieve the goal.

this is really tricky and simple and it's just created using CSS code.

here is an example: http://jsfiddle.net/hbirjand/RG8wW/

梦断已成空 2024-07-25 06:57:45
<a href="…" style="cursor: pointer;"><div> … </div></a>
<a href="…" style="cursor: pointer;"><div> … </div></a>
迷路的信 2024-07-25 06:57:45

这对我来说工作:

<div onclick="location.href='page.html';"  style="cursor:pointer;">...</div>

This work for me:

<div onclick="location.href='page.html';"  style="cursor:pointer;">...</div>
白衬杉格子梦 2024-07-25 06:57:45

您可以通过以下方法提供指向您的 div 的链接:

<div class="boxdiv" onClick="window.location.href='https://www.google.co.in/'">google</div>
<style type="text/css">
.boxdiv {
    cursor:pointer;
    width:200px;
    height:200px;
    background-color:#FF0000;
    color:#fff;
    text-align:center;
    font:13px/17px Arial, Helvetica, sans-serif;
    }
</style>

You can give a link to your div by following method:

<div class="boxdiv" onClick="window.location.href='https://www.google.co.in/'">google</div>
<style type="text/css">
.boxdiv {
    cursor:pointer;
    width:200px;
    height:200px;
    background-color:#FF0000;
    color:#fff;
    text-align:center;
    font:13px/17px Arial, Helvetica, sans-serif;
    }
</style>
青巷忧颜 2024-07-25 06:57:45

您可以使用 href 标签围绕元素,或者您可以使用 jquery 并使用

$('').click(function(e){
e.preventDefault();
//DO SOMETHING
});

You can make surround the element with a href tags or you can use jquery and use

$('').click(function(e){
e.preventDefault();
//DO SOMETHING
});
只怪假的太真实 2024-07-25 06:57:45

这是最简单的方法。

比如说,这是我想要使其可点击的 div 块:

<div class="inner_headL"></div>

所以按如下方式放置 href

<a href="#">
 <div class="inner_headL"></div>
</a>

只需将 div 块视为普通的 html元素并启用常用的 href 标记。

至少在 FF 上有效。

This is the simplest way.

Say, this is the div block I want to make clickable:

<div class="inner_headL"></div>

So put a href as follows:

<a href="#">
 <div class="inner_headL"></div>
</a>

Just consider the div block as a normal html element and enable the usual a href tag.

It works on FF at least.

冰葑 2024-07-25 06:57:45

我引入了一个变量,因为链接中的某些值会根据用户来自的记录而变化。
这适用于测试:

   <div onclick="location.href='page.html';"  style="cursor:pointer;">...</div> 

这也适用:

   <div onclick="location.href='<%=Webpage%>';"  style="cursor:pointer;">...</div> 

I pulled in a variable because some values in my link will change depending on what record the user is coming from.
This worked for testing :

   <div onclick="location.href='page.html';"  style="cursor:pointer;">...</div> 

and this works too :

   <div onclick="location.href='<%=Webpage%>';"  style="cursor:pointer;">...</div> 
桃酥萝莉 2024-07-25 06:57:45

虽然我不建议在任何情况下这样做,但这里有一些将 DIV 变成链接的代码(注意:此示例使用 jQuery,并且为了简单起见,删除了某些标记):

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
    $("div[href]").click(function () {
        window.location = $(this).attr("href");
    });
});

</script>
<div href="http://www.google.com">
     My Div Link
</div>

While I don't recommend doing this under any circumstance, here is some code that makes a DIV into a link (note: this example uses jQuery and certain markup is removed for simplicity):

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
    $("div[href]").click(function () {
        window.location = $(this).attr("href");
    });
});

</script>
<div href="http://www.google.com">
     My Div Link
</div>
抚笙 2024-07-25 06:57:45

div 包含在锚标记 内就像魅力一样:

    <a href="">
      <div>anything goes here will turn into a link</div>
    </a>

Enclosing your div inside an anchor tag <a href></a> works like charm:

    <a href="">
      <div>anything goes here will turn into a link</div>
    </a>
[旋木] 2024-07-25 06:57:45

如果您可以使用 bootstrap,一个简单的解决方案是使用 bootstrap .stretched-link

https://getbootstrap.com/docs/4.3/utilities/stretched-link/< /a>

示例代码

<div class="card" style="width: 18rem;">
  <img src="..." class="card-img-top" alt="...">
  <div class="card-body">
    <h5 class="card-title">Card with stretched link</h5>
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    <a href="#" class="btn btn-primary stretched-link">Go somewhere</a>
  </div>
</div>

If you can use bootstrap, one simple solution is to use bootstrap .stretched-link.

https://getbootstrap.com/docs/4.3/utilities/stretched-link/

Sample Code

<div class="card" style="width: 18rem;">
  <img src="..." class="card-img-top" alt="...">
  <div class="card-body">
    <h5 class="card-title">Card with stretched link</h5>
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    <a href="#" class="btn btn-primary stretched-link">Go somewhere</a>
  </div>
</div>
弥枳 2024-07-25 06:57:45

苏联的答案对我来说还不够。 我必须用来删除基线伪影。

a { display: inline-flex; }

当在 a 中仅使用 img 时,

Soviut's answer was not sufficient for me. I had to use

a { display: inline-flex; }

to remove baseline artifacts, when using just a img in the a.

宣告ˉ结束 2024-07-25 06:57:45

纯 HTML 和 CSS案例。 没有JS。

如果您想在完全可点击的 div 中包含其他链接元素。

主要思想是拉伸主要可点击链接,将其放置在内容下方,并使用 指针事件。 对于此元素内部的可点击元素,请为每个元素设置 pointer-events: all;

现场演示:

<div style="
            width: 300px; height: 300px;
            position: relative;
            border: 1px red solid;
            pointer-events: none;
            z-index: 0;
            ">
  <h1>No wrapper</h1>
  <p>This is fully clickable div, click handled by anchor tag. It contains no wrapper</p>
  <button>Learn more</button>

  <p>See this link ➡️ <a style="pointer-events: all;" title="Why I see this ad?" href="http://example.com/">?</a></p>

  <a style="
            position: absolute;
            top: 0;left: 0;right: 0; bottom: 0;
            text-indent: -9999px;overflow: hidden;
            pointer-events: all;
            z-index: -1;
            " title="GO TO YANDEX" href="https://ya.ru">CLICK</a>
</div>

Codepen:https://codepen.io/ColCh/pen/OJaPpJQ

Pure HTML & CSS case. No JS.

In case if you want to have other link elements inside of that fully clickable div.

Main idea is to stretch main clickable link, place it under the content, and disable clicks for content with pointer-events. For clickable elements inside of this element, set pointer-events: all; for each one.

Live demo:

<div style="
            width: 300px; height: 300px;
            position: relative;
            border: 1px red solid;
            pointer-events: none;
            z-index: 0;
            ">
  <h1>No wrapper</h1>
  <p>This is fully clickable div, click handled by anchor tag. It contains no wrapper</p>
  <button>Learn more</button>

  <p>See this link ➡️ <a style="pointer-events: all;" title="Why I see this ad?" href="http://example.com/">?</a></p>

  <a style="
            position: absolute;
            top: 0;left: 0;right: 0; bottom: 0;
            text-indent: -9999px;overflow: hidden;
            pointer-events: all;
            z-index: -1;
            " title="GO TO YANDEX" href="https://ya.ru">CLICK</a>
</div>

Codepen: https://codepen.io/ColCh/pen/OJaPpJQ

酷到爆炸 2024-07-25 06:57:45
 <div role="button"></div>
 <div role="button"></div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文