You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JavaScript点击notice_item打开新网页并关闭notice_dialog弹窗?

解决通知弹窗点击项关闭弹窗并跳转的问题

我来帮你搞定这个需求,咱们先明确要实现的核心功能:

  • 点击红色区域(.notice_item通知项)时,立刻关闭绿色区域(.notice_dialog弹窗),同时打开该通知项href指向的新网页
  • 点击show_dialog按钮可以正常打开/关闭弹窗,不受其他操作影响

问题分析

原代码的主要问题在于:没有针对.notice_item的点击事件做处理,同时弹窗的显示/隐藏逻辑绑定在父元素上容易误触发,还存在事件冒泡导致的冲突。下面是修改后的完整代码:


修改后的完整代码

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="notice_content">
  <button class="notice_center">
    show_dialog
  </button>
  <div class="notice_dialog">
    <div class="head">
      <h2 class="title">notice</h2>
      <button class="all_read">all read</button>
    </div>
    <ul class="nav">
      <button class="active">
        <p class="nav_link">generally</p><span class="notice_num">99+</span>
      </button>
      <button>
        <p class="nav_link">order</p><span class="notice_num">99+</span>
      </button>
    </ul>
    <ul class="notice_content_type" style="display: block">
      <li>
        <a class="notice_item unread" href="https://www.google.com" target="_blank">
          <div class="notice_item_content">
            <h3 class="info_title">GENERALLY TITLE</h3>
            <p class="info_main">CONTENT.....</p>
          </div>
        </a>
      </li>
      <li>
        <a class="notice_item" href="https://www.google.com" target="_blank">
          <div class="notice_item_content">
            <h3 class="info_title">GENERALLY TITLE</h3>
            <p class="info_main">CONTENT.....</p>
          </div>
        </a>
      </li>
      <li>
        <a class="notice_item unread" href="https://www.google.com" target="_blank">
          <div class="notice_item_content">
            <h3 class="info_title">GENERALLY TITLE</h3>
            <p class="info_main">CONTENT.....</p>
          </div>
        </a>
      </li>
      <li>
        <a class="notice_item" href="https://www.google.com" target="_blank">
          <div class="notice_item_content">
            <h3 class="info_title">GENERALLY TITLE</h3>
            <p class="info_main">CONTENT.....</p>
          </div>
        </a>
      </li>
    </ul>
    <ul class="notice_content_type">
      <li>
        <a class="notice_item" href="https://www.google.com" target="_blank">
          <div class="notice_item_content">
            <h3 class="info_title">ORDER TITLE</h3>
            <p class="info_main">CONTENT.....</p>
          </div>
        </a>
      </li>
      <li>
        <a class="notice_item" href="https://www.google.com" target="_blank">
          <div class="notice_item_content">
            <h3 class="info_title">ORDER TITLE</h3>
            <p class="info_main">CONTENT.....</p>
          </div>
        </a>
      </li>
      <li>
        <a class="notice_item" href="https://www.google.com" target="_blank">
          <div class="notice_item_content">
            <h3 class="info_title">ORDER TITLE</h3>
            <p class="info_main">CONTENT.....</p>
          </div>
        </a>
      </li>
    </ul>
  </div>
</div>

注意:我把通知项内部的div类名改成了notice_item_content,避免和外部的.notice_content重名导致样式或事件冲突,同时给a标签补充了完整的https://协议头,确保跳转正常。

CSS

body {
  padding: 30px;
}

.notice_content {
  position: relative;
}

.notice_content .notice_center {
  display: block;
  position: relative;
  background-repeat: no-repeat;
  background-size: contain;
  cursor: pointer;
}

.notice_content .notice_center .notice_center-num {
  display: inline-block;
  position: absolute;
  top: -8px;
  left: 8px;
  padding: 0px 4px;
  font-size: 12px;
  line-height: 1.5;
  border-radius: 12px;
  background-color: yellow;
  color: #222;
}

.notice_content .notice_dialog {
  display: none;
  width: 346px;
  max-height: 464px;
  position: absolute;
  top: 31px;
  left: 12px;
  z-index: 999;
  background-color: #fff;
  border-radius: 8px;
  border: 1px solid #222;
  box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.05);
  padding: 20px 0px 8px 0px;
}

.notice_content .notice_dialog .head {
  display: flex;
  justify-content: space-between;
  line-height: 1.5;
  letter-spacing: 0.56px;
  padding: 0px 24px 4px;
}

.notice_content .notice_dialog .head .title {
  font-size: 16px;
  font-weight: 500;
  margin: 0;
}

.notice_content .notice_dialog .head .all_read {
  font-size: 12px;
  letter-spacing: 1px;
  line-height: 1.5;
  cursor: pointer;
  border: none;
  background: none;
}

.notice_content .notice_dialog .nav {
  display: flex;
  padding: 0px 24px;
  border-bottom: 1px solid #ccc;
  margin: 0;
}

.notice_content .notice_dialog .nav button {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  cursor: pointer;
  border: none;
  background: none;
}

.notice_content .notice_dialog .nav button .nav_link {
  font-size: 14px;
  line-height: 1.5;
  margin: 0;
}

.notice_content .notice_dialog .nav button .notice_num {
  background-color: #ccc;
  color: #222;
  font-size: 12px;
  font-weight: 500;
  padding: 4px 6px 4px 6px;
  border-radius: 10px;
  margin-left: 8px;
}

.notice_content .notice_dialog .nav .active {
  border-bottom: 2px solid #222;
}

.notice_content .notice_dialog .nav .active .notice_num {
  color: #222;
  background-color: orange;
}

.notice_content .notice_dialog .notice_content_type {
  display: none;
  height: 336px;
  overflow-y: scroll;
  margin: 0;
  padding: 0;
}

.notice_content .notice_dialog .notice_content_type::-webkit-scrollbar {
  display: none;
}

.notice_content .notice_dialog .notice_content_type li {
  text-align: left;
  list-style: none;
}

.notice_content .notice_dialog .notice_content_type li .notice_item {
  display: flex;
  align-items: center;
  padding: 16px;
  text-align: left;
  line-height: 1.5;
  background-color: #ccc;
  border-bottom: 1px solid #222;
  text-decoration: none;
  color: inherit;
}

.notice_content .notice_dialog .notice_content_type li .notice_item:hover {
  background-color: yellow;
}

.notice_content .notice_dialog .notice_content_type li .notice_item .info_icon {
  width: 32px;
  height: 32px;
  margin-right: 16px;
}

.notice_content .notice_dialog .notice_content_type li .notice_item .info_title {
  font-size: 14px;
  font-weight: 500;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
  margin-top: 0;
}

.notice_content .notice_dialog .notice_content_type li .notice_item .info_main {
  font-size: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #222;
  margin-bottom: 6px;
  letter-spacing: 0.56px;
  margin-top: 0;
}

.notice_content .notice_dialog .notice_content_type li .notice_item .time {
  font-size: 12px;
  color: #ccc;
}

补充了一些基础样式(比如按钮的cursor、默认margin清除),让页面布局和交互更自然。

JavaScript

const Dialog = $(".notice_dialog");

// 点击show_dialog按钮切换弹窗显示/隐藏
$(".notice_center").on("click", function(e) {
  Dialog.toggle();
  e.stopPropagation(); // 阻止冒泡到document,避免刚打开就被关闭
});

// 点击弹窗外部关闭弹窗
$(document).on("click", function(e) {
  if (!e.target.closest(".notice_dialog")) {
    Dialog.hide();
  }
});

// 点击通知项时关闭弹窗,同时允许a标签正常跳转
$(".notice_item").on("click", function(e) {
  Dialog.hide();
  // 如果需要自定义跳转逻辑,可以替换成下面的代码:
  // window.open(this.href, this.target);
  // e.preventDefault(); // 注释掉原a标签的默认行为
});

// Tab切换逻辑(保留原功能,添加阻止冒泡)
let nav = document.querySelector(".nav");
let buttons = nav.querySelectorAll("button");
let contents = document.querySelectorAll(".notice_content_type");

for (let i = 0; i < buttons.length; i++) {
  buttons[i].setAttribute("data-index", i);
  buttons[i].onclick = function(e) {
    e.stopPropagation(); // 阻止冒泡到document,避免弹窗关闭
    for (let i = 0; i < buttons.length; i++) {
      buttons[i].className = "";
    }
    this.className = "active";
    let index = this.getAttribute("data-index");
    for (let i = 0; i < contents.length; i++) {
      contents[i].style.display = "none";
    }
    contents[index].style.display = "block";
  };
}

关键修改说明

  1. 精准绑定弹窗触发事件:把弹窗的显示/隐藏逻辑从.notice_content父元素改为直接绑定到.notice_center按钮,避免点击弹窗内部区域误触发弹窗切换。
  2. 添加通知项点击处理:给.notice_item添加点击事件,先关闭弹窗,然后让a标签的默认跳转行为生效(如果需要自定义跳转,可以用window.open)。
  3. 修复事件冒泡冲突:在弹窗内部的所有交互事件(按钮点击、通知项点击)中添加e.stopPropagation(),防止触发document的全局隐藏逻辑导致弹窗意外关闭。
  4. 修复类名与路径问题:修改了通知项内部的重复类名,给a标签补充了完整的协议头,确保跳转正常。

这样修改后,就能完美实现你想要的功能啦!

内容的提问来源于stack exchange,提问作者AWEI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 10:05:24