在JavaScript中排除DOM的sub div

发布于 2025-02-03 03:23:14 字数 2192 浏览 3 评论 0 原文

我有两个分区。 < div class =“ card” id =“ openwebsite”> 和sub div < div class =“ card__btn”>

如果有人单击我的根div,将被重定向到 example.com ,当某人单击sub div时,它们将重定向到 example.com & 示例,他们在两个新选项卡中重定向两个链接。

我想要,如果有人单击root div,他们应该将其重定向到 example.com ,当某人单击sub div按钮时,他们只能将其重定向到 example.net not not代码> example.com

如何排除sub div < div class =“ card__btn”> 从javaScript中。

代码 :

const webPage = document.querySelectorAll("#openWebsite");
webPage.forEach((e) => {
  e.addEventListener("click", () => {
    const site = e.getAttribute("data-site");
    window.open(site, "_blank") || window.location.replace(site);
  });
});
.card {

background: blue;
height: 100px;
width: 500px;

}
<div class="card" id="openWebsite" data-site="https://example.com">
    <div class="card__btn">
      <a href="https://example.net">
        Visit Site
      </a>
    </div>
  </div>

已经从 jQuery排除了selector ,jquery , https://api.jquery.com/not/ document.document.document.document.queryselector排除Dom 的一部分/MET_DOCUMEMENT_QUERYSELECTORALL.ASP“ RER =“ Nofollow Noreferrer”> https://www.w3schools.com/jsref/met_document_queryselectorallectorall.asp

document.querySelectorAll("#openWebsite").not(".card__btn");

document.querySelectorAll(".card").not(".card__btn");

document.querySelectorAll("#openWebsite:not('.card__btn')");

I have two div. <div class="card" id="openWebsite"> and sub div <div class="card__btn">

If someone clicks on my root div they will get redirected to example.com and when someone clicks sub div they are redirected to example.com & example.net, they are getting redirected two both links in two new tab.

I want, if someone clicks on root div they should get redirected to example.com and when someone clicks on sub div button they should only be redirected to example.net not example.com

how to exclude sub div <div class="card__btn"> from javascript.

Code :

const webPage = document.querySelectorAll("#openWebsite");
webPage.forEach((e) => {
  e.addEventListener("click", () => {
    const site = e.getAttribute("data-site");
    window.open(site, "_blank") || window.location.replace(site);
  });
});
.card {

background: blue;
height: 100px;
width: 500px;

}
<div class="card" id="openWebsite" data-site="https://example.com">
    <div class="card__btn">
      <a href="https://example.net">
        Visit Site
      </a>
    </div>
  </div>

Already tried from jQuery exclude elements with certain class in selector , https://api.jquery.com/not/ , document.querySelector exclude part of DOM, https://www.w3schools.com/jsref/met_document_queryselectorall.asp

document.querySelectorAll("#openWebsite").not(".card__btn");

and

document.querySelectorAll(".card").not(".card__btn");

and

document.querySelectorAll("#openWebsite:not('.card__btn')");

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

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

发布评论

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

评论(3

旧梦荧光笔 2025-02-10 03:23:14

您需要使您的内部DIV位置绝对,然后它将按照您想要的方式工作。

You need to make you internal div position absolute then it will work the way you want.

離人涙 2025-02-10 03:23:14

那里
您的代码绝对可以,只是您

querySelectorAll()

仅用的代替

querySelector()

您使用的代码,因为您在页面上只有一个ID具有“ openwebsite”的ID,

下面是我相信您可以添加到CSS的建议,以使您的项目更加可见

    .card {
       background: blue;
       height: 100px;
       width: 500px;
    }
    .card__btn {
       background-color: red;
       padding: 10px;
      }
    a {
       color: white;
      }

there
your code is absolutely fine, just that you used

querySelectorAll()

instead of

querySelector()

only, because you only have one id on your page with the id "openWebsite",

below is a suggestion I believe you can add to your css to make your project more visible

    .card {
       background: blue;
       height: 100px;
       width: 500px;
    }
    .card__btn {
       background-color: red;
       padding: 10px;
      }
    a {
       color: white;
      }
又怨 2025-02-10 03:23:14

事件在DOM上冒泡。单击锚时,它将打开示例.net
然后它上升到树,您的事件处理程序被触发,导致 example.com 要打开。
要停止此操作,您可以使用”代码> 。这样,事件将不会继续爬上树。

因为Stackoverflow不允许打开新的选项卡,所以我还创建了a fiddle

const webPage = document.querySelectorAll("#openWebsite");
webPage.forEach((e) => {
  e.addEventListener("click", () => {
    const site = e.getAttribute("data-site");
    window.open(site, "_blank") || window.location.replace(site);
  });
});

document.querySelector(".card__btn").addEventListener("click", event => {
  event.stopPropagation();
});
.card {
  background: blue;
  height: 100px;
  width: 500px;
}
<div class="card" id="openWebsite" data-site="https://example.com">
  <div class="card__btn">
    <a href="https://example.net">
        Visit Site
      </a>
  </div>
</div>

Events bubble up the dom. When you click on the anchor it opens example.net.
Then it goes up the tree and your event handler is triggered, causing example.com to be opened.
To stop this you can use Event.stopPropagation. That way, the event won't continue going up the tree.

Because stackoverflow doesn't allow new tabs to be opened, I have also created a fiddle.

const webPage = document.querySelectorAll("#openWebsite");
webPage.forEach((e) => {
  e.addEventListener("click", () => {
    const site = e.getAttribute("data-site");
    window.open(site, "_blank") || window.location.replace(site);
  });
});

document.querySelector(".card__btn").addEventListener("click", event => {
  event.stopPropagation();
});
.card {
  background: blue;
  height: 100px;
  width: 500px;
}
<div class="card" id="openWebsite" data-site="https://example.com">
  <div class="card__btn">
    <a href="https://example.net">
        Visit Site
      </a>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文