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

点击弹出框Add Card链接时动态添加带按钮的textarea实现方案

实现点击Popover中“Add Card”添加带按钮的文本框功能

嘿,我来帮你搞定这个需求!作为JavaScript新手,咱们一步步来实现每次点击「Add Card」就添加带按钮的文本框~

核心思路

咱们需要给「Add Card」链接绑定点击事件,每次触发时动态创建一个包含文本框和按钮的元素,然后把它添加到页面的指定位置(比如你的「Stuff To Try」列表下方)。

修改后的完整代码

下面是调整后的代码,我会标注关键修改点:

HTML 部分

<div class="flex-container">
  <div class="parent2">
    <span><b>Stuff To Try (this is a list)</b></span>
    <span>
      <i class="fa fa-ellipsis-h" title="List Actions" tabindex="0" data-trigger="focus" data-toggle="popover"></i>
    </span>
    <!-- 新增:用来放置新添加的卡片容器 -->
    <div id="cardsContainer" class="cards-container"></div>
  </div>

  <div id="popover-content" style="display:none;">
    <ul style="margin:0; padding:0; list-style: none;" class="pop-over-list">
      <!-- 关键修改:给Add Card链接加唯一id,方便绑定事件 -->
      <li>
        <a href="#" id="addCardLink">
          <b style="color: #444444">Add Card...</b>
        </a>
      </li>
      <li>
        <a href="#">
          <b style="color: #444444">Copy List...</b>
        </a>
      </li>
      <li>
        <a href="#"><b style="color: #444444"> Move List...</b></a>
      </li>
      <li>
        <a href="#"><b style="color: #444444">Suscribe...</b></a>
      </li>
    </ul>
    <hr>
    <ul style="margin:0; padding:0; list-style: none;" class="pop-over-list">
      <li>
        <a href="#">
          <b style="color: #444444">Move All Cards In This List...</b>
        </a>
      </li>
      <li>
        <a href="#"><b style="color: #444444"> Archive All Cards in this List...</b></a>
      </li>
    </ul>
    <hr>
    <ul style="margin-top:0px; padding:0; list-style: none;" class="pop-over-list">
      <li><a href="#"> <b style="color: #444444">Sort By Date Created (Newest First)...</b> </a></li>
      <li><a href="#"><b style="color: #444444"> Sort By Date Created (Oldest First)...</b> </a></li>
    </ul>
    <hr>
    <ul style="margin-top:2px; padding:0; list-style: none;" class="pop-over-list">
      <li><a href="#"> <b style="color: #444444">Archive This List...</b> </a></li>
    </ul>
  </div>
</div>

CSS 部分(新增卡片的样式,让布局更美观)

.pop-over-list li>a {
  display: block;
  font-weight: 700;
  padding-top:6px;
  position: relative;
  margin: 0 -10px;
  text-decoration: none;
}
.popover-content li>a:hover{
  background-color: #298fca;
}
.parent2 {
  display: flex;
  justify-content: space-between;
  flex-direction: column; /* 新增:让列表和卡片容器垂直排列 */
  gap: 10px; /* 新增:增加间距 */
}
/* 新增:卡片容器和文本框的样式 */
.cards-container {
  margin-top: 10px;
}
.card-item {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}
.card-item textarea {
  flex: 1;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
.card-item button {
  padding: 0 16px;
  background-color: #298fca;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.card-item button:hover {
  background-color: #1a6fa0;
}

JavaScript 部分(核心功能实现)

$(document).ready(function(){
  // 初始化popover
  $('[data-toggle="popover"]').popover({
    placement : 'right',
    html: true,
    content: function() {
      return $('#popover-content').html();
    }
  });

  $('.popover-dismiss').popover({ trigger: 'focus' });

  // 关键:绑定Add Card的点击事件
  $(document).on('click', '#addCardLink', function(e){
    e.preventDefault(); // 阻止链接默认跳转行为
    // 创建带按钮的文本框元素
    const cardHtml = `
      <div class="card-item">
        <textarea placeholder="输入卡片内容..."></textarea>
        <button class="saveCardBtn">保存</button>
      </div>
    `;
    // 添加到卡片容器中
    $('#cardsContainer').append(cardHtml);
    // 关闭popover(可选,点击后自动关闭弹窗)
    $('[data-toggle="popover"]').popover('hide');
  });

  // 绑定保存按钮的点击事件(处理文本框内容)
  $(document).on('click', '.saveCardBtn', function(){
    const textarea = $(this).siblings('textarea');
    const cardContent = textarea.val().trim();
    if(cardContent){
      // 这里可以添加保存逻辑,比如把内容存入数组、发送到后端等
      alert(`卡片内容:${cardContent}`);
      // 可选:保存后替换为只读文本
      textarea.replaceWith(`<div class="card-content">${cardContent}</div>`);
      $(this).remove();
    } else {
      alert('请输入卡片内容~');
    }
  });
});

关键知识点解释

  • 事件委托:用$(document).on('click', '#addCardLink', ...)而不是直接$('#addCardLink').click(),因为popover的内容是动态生成的,页面加载时它不在DOM里,事件委托能确保动态元素也能触发事件。
  • 阻止默认行为:e.preventDefault()避免点击链接时页面跳转。
  • 动态创建元素:用字符串模板生成HTML,然后通过append()添加到页面。
  • popover关闭:点击Add Card后调用popover('hide')自动关闭弹窗,提升用户体验。

这样每次点击popover里的「Add Card」,就会在列表下方添加一个带保存按钮的文本框啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:10:45