简体   繁体   English

错误的链接,错误的地方

[英]wrong link, wrong place

ok so I'm trying to display news articles from an api. 好的,所以我试图显示来自api的新闻报道。 what I've written in javascript does just that (article photo, title, author, etc) except for the links to each particular article. 我用javascript编写的内容(文章照片,标题,作者等)仅能做到这一点,但每个特定文章的链接除外。 at first they werent clickable at all, then I changed the css with something I found here. 起初它们根本不可点击,然后我用在这里找到的东西更改了CSS。 that made the whole entire page clickable and only applies to one link, and one link only. 使整个页面可点击,并且仅适用于一个链接和一个链接。 so what I need to know is how to make sure the clicks happen in the right place and go to the right location? 因此,我需要知道的是如何确保点击发生在正确的位置并到达正确的位置? please and thank you. 谢谢,麻烦您了。

function GenerateArticle(data) {
  const Article = document.getElementById("ArticleGrid")
  for (let i = 0; i < data.length; i++) {
    const Div = document.createElement("Article");
    Div.innerHTML =
      `<img src = ${data[i].urlToImage}>
            <h5>${data[i].title}</h5>
            <p>${data[i].publishedAt}</p>
            <p>${data[i].description}</p>
            <p>${data[i].content}</p>
            <p>${data[i].source.name}</p>
            <p>${data[i].author}</p>
            <a href = '${data[i].url}'></a>`;
    Article.appendChild(Div);
  }
}
a {
  position: fixed;
  display: block;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  text-decoration: none;
}

What you're trying to achieve is a stretched link, an anchor that has no actual content but spans the entire contents of it's parent node. 您想要实现的是拉伸链接,即没有实际内容的锚点,但跨越了其父节点的全部内容。 Only, you're not quite there. 只是,您并不在那里。 A few ways to do this but let's simplify for demonstration. 有几种方法可以做到这一点,但让我们简化一下进行演示。

Set the position style property on the Div to relative. Div上的位置样式属性设置为相对。

const Div = document.createElement("article");
Div.style.position = "relative";

Absolute position your link in place of fixed. 绝对将您的链接放置在固定位置。

a {
  position: absolute;
  display: block;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  text-decoration: none;
}

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

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