将div变成链接
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
来到这里是希望找到一个更好的解决方案,但我不喜欢这里提供的任何解决方案。 我认为你们中有些人误解了这个问题。 OP 想让一个充满内容的 div 表现得像一个链接。 Facebook 广告就是一个例子 - 如果你仔细观察,它们实际上是正确的标记。
对我来说,禁忌是: javascript(不应该仅仅为了链接而需要,而且搜索引擎优化/可访问性非常糟糕); 无效的 HTML。
本质上是这样的:
,而不是
- 感谢 @Campey)
将以下 CSS 应用到空范围:
<前><代码>{
位置:绝对;
宽度:100%;
高度:100%;
顶部:0;
左:0;
z 索引:1;
/* 修复 IE7/8 中的重叠错误,
确保你有一个空的 gif */
背景图像: url('empty.gif');
}
它现在将覆盖面板,并且由于它位于
标记内,因此它是一个可点击的链接
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:
<span></span>
, not<span />
- thanks @Campey)apply the following CSS to the empty span:
It will now cover the panel, and as it's inside an
<A>
tag, it's a clickable link您不能使
div
本身成为链接,但可以使标记充当
block
,行为相同有。
然后您可以设置其宽度和高度。
You can't make the
div
a link itself, but you can make an<a>
tag act as ablock
, the same behaviour a<div>
has.You can then set the width and height on it.
这是一个古老的问题,但我想我会回答它,因为这里的每个人都有一些疯狂的解决方案。 它实际上非常非常简单...
锚标记的工作原理是这样的 -
Sooo...
虽然我不确定这是否有效。 如果这就是口头解决方案背后的原因,那么我深表歉意......
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 -
Sooo...
Although I'm not sure if this is valid. If that's the reasoning behind spoken solutions, then I apologise...
需要一点 JavaScript。
但是,您的
div
将是可点击的。Requires a little javascript.
But, your
div
would be clickable.此选项不需要空.gif,如最受支持的答案中所示:
HTML:
CSS:
按照 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:
CSS:
As proposed at http://www.digitalskydesign.com/how-to-make-an-entire-div-a-link-using-css/
最干净的方法是使用 jQuery 和 HTML 中引入的数据标签。 使用此解决方案,您可以在您想要的每个标签上创建链接。 首先使用数据链接标签定义标签(例如div):
现在您可以根据需要设置div 的样式。 并且您还必须为“链接”类似行为创建样式:
最后将此 jQuery 调用放入页面:
使用此代码,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:
Now you can style the div however you want. And you have to create also the style for the "link"-alike behavior:
And at last put this jQuery call to the page:
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.
这是实现您想要的目标的“有效”解决方案。
但很可能您真正想要的是将
标记显示为块级元素。
我不建议使用 JavaScript 来模拟超链接,因为这违背了标记验证的目的,而标记验证的最终目的是促进可访问性(遵循正确的语义规则发布格式良好的文档可以最大限度地减少不同浏览器对同一文档进行不同解释的可能性)。
最好发布一个未经验证的网页,但可以在所有浏览器上正常呈现和运行,包括禁用 JavaScript 的浏览器。 此外,使用
onclick
不会为屏幕阅读器提供语义信息来确定 div 是否充当链接。This is a "valid" solution to achieving what you want.
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.不确定这是否有效,但它对我有用。
代码 :
Not sure if this is valid but it worked for me.
The code :
为了使同行的答案在 IE 7 及更高版本中工作,需要进行一些调整。
如果元素没有背景颜色,IE 将不支持 z-index,因此链接不会重叠包含内容的 div 的部分,只会重叠空白部分。 为了解决这个问题,添加了不透明度为 0 的背景。
出于某种原因,在链接方法中使用跨度时,IE7 和各种兼容模式完全失败。 然而,如果链接本身被赋予了样式,它就可以正常工作。
To make thepeer's answer work in IE 7 and forward, it needs a few tweaks.
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.
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.
您还可以尝试包裹一个锚点,然后将其高度和宽度与其父级相同。 这对我来说非常有效。
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.
没有提到的一个选项是使用 flex。 通过将
flex: 1
应用于a
标记,它会扩展以适合容器。An option that hasn't been mentioned is using flex. By applying
flex: 1
to thea
tag, it expands to fit the container.这对我有用:
HTML:
CSS:
这添加了一个不可见元素(跨度),它覆盖了整个 div,并且位于 z 索引上的整个 div 之上,因此当有人单击该元素时div,点击本质上是被链接的不可见的“span”层拦截的。
注意:如果您已经对其他元素使用 z 索引,只需确保此 z 索引的值高于您希望其“位于顶部”的任何值。
This worked for me:
HTML:
CSS:
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.
为什么不?
使用
在 HTML5 中工作正常
why not?
use <a href="bla"> <div></div> </a>
works fine in HTML5这个例子对我有用:
This example worked for me:
我知道这篇文章很旧,但我只需要解决同样的问题,因为简单地编写一个正常的链接标签并将显示设置为阻止并不会使整个 div 在 IE 中可点击。 因此解决这个问题比使用 JQuery 简单得多。
首先让我们了解为什么会发生这种情况:IE 不会使空 div 可点击,它只会使该 div/a 标签内的文本/图像可点击。
解决方案:用背景图像填充 div 并将其对查看者隐藏。
如何?
你问了好问题,现在听着。
将这个背景样式添加到 a 标签中
,这样整个 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
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!
只需将链接放在块中并使用 jquery 对其进行增强即可。 对于任何没有 JavaScript 的人来说,它可以 100% 优雅地降级。 恕我直言,用 html 来做这件事并不是最好的解决方案。
例如:
然后使用jquery使块可点击(通过网页设计师墙):
那么您所要做的就是将光标样式添加到 div 中。
为了获得奖励积分,仅当通过向 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:
Then use jquery to make the block clickable (via web designer wall):
Then all you have to do is add cursor styles to the div
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.
这是 BBC 网站和卫报上使用的最佳方法:
我在这里找到了该技术:
http://codepen.io/IschaGast/pen/Qjxpxo
这里是html,
这里是CSS
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
heres the CSS
实际上你现在需要包含 JavaScript 代码,
请查看本教程来执行此操作。
但有一个棘手的方法可以使用 CSS 代码来实现这一点
您必须在 div 标签内嵌套一个锚标记,并且必须向其应用此属性,
当您这样做时,如果您想覆盖,它将使整个宽度区域可点击(但在锚标记的高度内)整个 div 区域,您必须将锚标记的高度精确设置为 div 标记的高度,例如:
这将使整个区域可点击,然后您可以应用
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,
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:
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/
这对我来说工作:
This work for me:
您可以通过以下方法提供指向您的 div 的链接:
You can give a link to your div by following method:
您可以使用 href 标签围绕元素,或者您可以使用 jquery 并使用
You can make surround the element with a href tags or you can use jquery and use
这是最简单的方法。
比如说,这是我想要使其可点击的
div
块:所以按如下方式放置
href
:只需将
div
块视为普通的 html元素并启用常用的href
标记。至少在 FF 上有效。
This is the simplest way.
Say, this is the
div
block I want to make clickable:So put a
href
as follows:Just consider the
div
block as a normal html element and enable the usual ahref
tag.It works on FF at least.
我引入了一个变量,因为链接中的某些值会根据用户来自的记录而变化。
这适用于测试:
这也适用:
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 :
and this works too :
虽然我不建议在任何情况下这样做,但这里有一些将 DIV 变成链接的代码(注意:此示例使用 jQuery,并且为了简单起见,删除了某些标记):
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):
将
div
包含在锚标记内就像魅力一样:
Enclosing your
div
inside an anchor tag<a href></a>
works like charm:如果您可以使用 bootstrap,一个简单的解决方案是使用 bootstrap
.stretched-link
。https://getbootstrap.com/docs/4.3/utilities/stretched-link/< /a>
示例代码
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
苏联的答案对我来说还不够。 我必须用来删除基线伪影。
当在
a
中仅使用img
时,Soviut's answer was not sufficient for me. I had to use
to remove baseline artifacts, when using just a
img
in thea
.纯 HTML 和 CSS案例。 没有JS。
如果您想在完全可点击的 div 中包含其他链接元素。
主要思想是拉伸主要可点击链接,将其放置在内容下方,并使用 指针事件。 对于此元素内部的可点击元素,请为每个元素设置
pointer-events: all;
。现场演示:
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:
Codepen: https://codepen.io/ColCh/pen/OJaPpJQ