简体   繁体   English

如何在没有“a”标签的情况下将“div”设为“a href”?

[英]How can I make "div" as "a href" without "a" tag?

In Wired they using a card as a link, like Image .Wired 中,他们使用卡片作为链接,例如Image

So, I tried "document.location.href" script on div.所以,我在 div 上尝试了“document.location.href”脚本。 but it isn't display a link like Image .但它不显示像Image这样的链接。

and also click with command key doesn't work.并且用命令键单击也不起作用。

How can I make div as a href using script?如何使用脚本将 div 设为 href?

在你的 div 标签中试试这个:

onClick="window.open('href');"

You can do it with JS.你可以用JS做到这一点。

HTML HTML

<div id='div-id'>Click Here</div>

JS JS

document.getElementById('div-id').onclick = function(){window.open('http://url.url')};

Note: the link won't actually work inside of the StackOverflow snippet below because it's inside of a sandboxed iframe , but this code otherwise will work in normal contexts:注意:该链接实际上不会在下面的 StackOverflow 片段内工作,因为它在沙盒iframe ,但此代码将在正常上下文中工作:

 div { cursor: pointer; border: 2px solid gray; border-radius: 4px; height: 200px; width: 200px; display: flex; justify-content: center; align-items: center; }
 <div onclick="window.open('https://www.wired.com')">click me to go to wired</div>

Actually, WIRED simply wraps the card image and text is anchor tags and applies styling to change the hover event, like so.实际上,WIRED 只是简单地包装卡片图像,文本是锚标记并应用样式来更改悬停事件,就像这样。

.card {
  width: 300px;
  height: 450px;
  background: #eee;
  border-radius: 6px;
  border: 1px #ccc solid;
  margin-top: 30px;
}

.card:hover {
  transform: translateY(-5%);
  transition: transform 350ms ease-out;
}

.card-link {
  margin: 12px 5px;
  font-family: "Roboto", san-serif;
  font-size: 1.3em;
  text-align: center;
  display: block;
}

a.card-link {
  text-decoration: none;
  color: #595959;
}

a:hover.card-link {
  color: #999999;
  transition: color 200ms ease-in;
}

.card-link-image > img {
  display: block;
  margin: 5px auto 15px;
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  box-shadow: 2px 2px 1px #595959;
}

.card-link-image > img:hover {
  box-shadow: 3px 3px 1px #333333;
  transform: translateX(-1px);
  transition: transform 100ms ease-in;
}

<div class="card">
  <a class="card-link-image" to="https://google.com"><img src="https://source.unsplash.com/285x285" alt="image"/></a>
  <a class="card-link" href="https://google.com">Link 1</a>
  <a class="card-link" href="https://google.com">Link 2
        </a>
</div>

you can see it in example is this codepen .你可以在例子中看到它是这个 codepen And while its very much possible to use a div as a link using the methods in both Guy L, kdedorov91, and dauzduz TV answers, I wouldn't recommend doing it.虽然使用 Guy L、kdedorov91 和 dauzduz TV 答案中的方法使用div作为link很有可能,但我不建议这样做。

Every element has a role it plays in the page and the browser has a tendency to takes offense when a tag isn't being used properly in the form of error and warning messages in the console.每个元素在页面中都扮演着自己的角色,当标签没有以控制台中的错误和警告消息的形式被正确使用时,浏览器往往会受到冒犯。

If you do decide to use one of the aforementioned answers be sure to set the role attribute on the div to link .如果您决定使用上述答案之一,请务必将div上的role属性设置为link

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

相关问题 如何指定锚标记的行为类似于超链接,没有href和锚名称? - How can I specify an anchor tag behaves like a hyperlink, without an href and without an anchor name? 我怎样才能将 append 地址<a>标记为 href</a> - How can I append address to <a> tag href 如何使Div标签可见和不可见? - How can I make the Div tag visible and invisible.? 当图像已经具有href标签时,如何使它成为链接? - How can I make an image a link when it already has an a href tag? 我该怎么做<img>点击时标签显示全尺寸图像,而该标签的href可以更改? - How can I make <img> tag show a full size image when clicked, while href of that tag can change? 我如何<a href>发出其GET请求而不进行重定向?</a> - How can I make <a href> make its GET request without redirecting? 使Div可单击而不使用Javascript <a>或href</a> - Make Div Clickable without Javascript or <a> or href 在单独的div标签中单击时,如何使每个标签显示不同的文本块? - How can I make each a tag display a different chunk of text when clicked in a separate div tag? 如何在不单击 div 的情况下单击可点击 div(ng-click)内的标签? - How can I click a tag inside of clickable div(ng-click) without clicking the div? 如何删除div标签下列出的href? - How to remove a href that is listed under div tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM