【问题标题】:How to take a href attribute and use it in a div when clicking on it?单击时如何获取href属性并在div中使用它?
【发布时间】:2020-02-07 01:41:26
【问题描述】:

在javascript中我想告诉post-container,当我点击它时,获取位于link的href并转到第X页。

有多个post-container

我的代码是这样的:

<div class="post-container">
  <a class="link" href="X">
    <h2 class="post-title">Hello, world!</h2>
  </a>
  <div class="date-posts">...</div>
</div>

我希望能够在post-container的任何区域中点击我的帖子的Hello, world!标题之外并转到第X页

知道怎么做吗?

【问题讨论】:

  • 您尝试了哪些方法,又是如何失败的?
  • 谷歌addEventListenerlocation + javascript
  • 如果点击insideHello, world!会发生什么?

标签: javascript html href


【解决方案1】:

Javascript

var elements = document.getElementsByClassName('post-container');

var loadLink = function() {

    var link = this.getElementsByTagName("a")[0].href;

    window.location.href = link;

};

for (var i = 0; i < elements.length; i++) {
    elements[i].addEventListener('click', loadLink, false);
}

查看working demo

【讨论】:

  • 你的 vanilla JS 只适用于第一个 .post-container
【解决方案2】:

假设您有多个.post-containers,您需要遍历它们中的每一个并添加一个事件侦听器来处理点击事件。点击后,找到a 获取其href 并设置window.location(我只是在此演示中通过控制台登录)

(function(){
    let elems = document.querySelectorAll(".post-container");
    for (i = 0; i < elems.length; ++i) {
        let el = elems[i];
        el.addEventListener("click", function(e) {
            e.preventDefault();
            let href = el.querySelector("a").getAttribute("href");
            console.log("href",href);
            //window.location.href = href;
        })
    }
})();
<div class="post-container">
  <a class="link" href="X">
    <h2 class="post-title">Hello, world!</h2>
  </a>
  <div class="date-posts">...</div>
</div>
<div class="post-container">
  <a class="link" href="Y">
    <h2 class="post-title">Goodbye world!</h2>
  </a>
  <div class="date-posts">...</div>
</div>

【讨论】:

【解决方案3】:

这将起作用。将事件侦听器添加到父元素(Event Delegation)。您可以获取子元素的href 属性,然后使用window.location.href。您可能需要添加preventDefault() 以避免默认a 标签的行为

如果您有多个具有相同类名的标签。您必须使用querySelectorAll。如果您有单个元素或多个具有相同类名的元素,请使用querySelctorAll

// if you have one tag
let ele = document.querySelectorAll(".post-container")
ele.forEach(element => {
  element.addEventListener("click", function(e) {
    e.preventDefault();
    let href = element.children[0].getAttribute("href")
    window.location.href = href;

  })

})
.post-container {
  background-color: green;
}
<div class="post-container">
  <a class="link" href="X">
    <h2 class="post-title">Hello, world!</h2>
  </a>
  <div class="date-posts">...</div>
</div>

【讨论】:

  • 谢谢。很好的帮助:)
  • @SebastiánVarellaGmz 如果post-container 类有两个元素,此解决方案将不起作用
  • @Āchahms 我分享了我提出的一个问题,以便您更好地理解我的问题。 stackoverflow.com/questions/58295443/post-clickable-in-blogger
  • @Shubh 能否为我提供多个post-container 的解决方案?
  • 我已经做过了。它适用于多个帖子容器。更新的解决方案
【解决方案4】:
document.getElementsByClassName('post-container')[0].onclick = function(e){
  location.href = this.children[0].href;
}

【讨论】:

  • HTMLCollection 没有 onclick 属性。
  • 你也忘记了s...如果有多个post-container怎么办?
  • 如果有多个,那么我会采用不同的解决方案。这超出了要求的范围。
  • @CodyFortenberry 能否为我提供多个post-container 的解决方案?
  • @SebastiánVarellaGmz 只需一个简单的循环即可。 jsfiddle.net/8nmqzth5 除此之外,我可能会使用 querySelectorAll,因为它有一个原生的 forEach 函数。
【解决方案5】:

我希望我正确地解释了您的问题,但是您可以重新安排编写 href 标记的位置。相反,将您的 div 包装在其中。这将使整个 div 元素成为一个链接。

<a class="link" href="google.com">
    <div class="post-container">
        <h2 class="post-title">Hello, World!</h2>
        <div class="date-posts"></div>
    </div>
</a>

【讨论】:

  • 我按照你的建议做了,但在我的情况下它不起作用。我分享了我提出的一个问题,以便您更好地理解我的问题。 stackoverflow.com/questions/58295443/post-clickable-in-blogger
  • Sebastián Varella Gmz 如果你能做到,那就行了。另一个问题类似但不同,答案有其他考虑。您可能想将 JavaScript onclick 事件添加到 div 并忘记 a,例如 &lt;div onclick="window.location='#';"&gt;
猜你喜欢
  • 2011-10-01
  • 2017-04-02
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 2019-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多