按下按钮时,访问Div A按钮包裹

发布于 2025-02-06 12:03:48 字数 873 浏览 1 评论 0原文

我目前正在设计一个将加载在可用项目列表中的网站。我正在使用EJS从数据库中动态加载项目并将其显示在单独的DIV中。但是,我希望用户能够从列表中选择其中一个项目。我已经在每个Divs上添加了一个按钮,但是我不确定如何制作它,以便按下按钮时,它访问了DIV的嵌套。

在下面


    <%for(var i = 0; i<groups.length; i++){%>
    <div class="col-md-4">
      <div class="card rider-card">
        <div class="card-body">
          <h4 class="card-title">Text Here</h4>
          <h5 class="card-subtitle mb-2 text-muted">Text Here</h5>
          <h6 class="card-text">___ spots available</h6>
          <p class="card-text">Text Here</p>
          <a onclick="selectDriver()" class="stretched-link"></a>
        </div>
      </div>
    </div>
    <%}%>

代码 数据库中的数据,并将集合称为组。我迭代组并为每个组创建一张新卡。因为我正在动态生成Divs,所以我似乎找不到一种区分它们的方法。该按钮是锚标签,适用于整个卡。

有没有办法仅访问锚标签嵌套的卡?

I'm currently designing a website that loads in a list of available items. I'm using EJS to dynamically load the items in from a database and displaying them in separate divs. However, I want the user to be able to select one of the items from the list. I've added a button to each of the divs, but I'm not sure how to make it so that when a button is pressed, it accesses the div it is nested in.

The code is below


    <%for(var i = 0; i<groups.length; i++){%>
    <div class="col-md-4">
      <div class="card rider-card">
        <div class="card-body">
          <h4 class="card-title">Text Here</h4>
          <h5 class="card-subtitle mb-2 text-muted">Text Here</h5>
          <h6 class="card-text">___ spots available</h6>
          <p class="card-text">Text Here</p>
          <a onclick="selectDriver()" class="stretched-link"></a>
        </div>
      </div>
    </div>
    <%}%>

I'm using MongoDB to store the data in the database, and calling the collection as groups. I iterate through the groups and create a new card for each one. Because I'm dynamically generating the divs, I can't seem to find a way to differentiate them. The button is the anchor tag, which applies to the entire card.

Is there a way to access only the card the anchor tag is nested in?

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

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

发布评论

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

评论(1

断舍离 2025-02-13 12:03:48

您可以使用模板语言为每张卡创建一个逐渐增加的ID,使用您的“ for”循环中的i变量:

<%for(var i = 0; i<groups.length; i++){%>
<div class="col-md-4">
  <div class="card rider-card">
    <div class="card-body" id="card_<%=i%>">
      <h4 class="card-title">Text Here</h4>
      <h5 class="card-subtitle mb-2 text-muted">Text Here</h5>
      <h6 class="card-text">___ spots available</h6>
      <p class="card-text">Text Here</p>
      <a onclick="selectDriver(document.getElementById('card_<%=i%>))" class="stretched-link"></a>
    </div>
</div>
</div>
<%}%>

如果您想在其他地方引用该卡,这很有用。或者,您可以通过parenthnodes冒泡并钩住上方的div

      <a onclick="selectDriver(this.parentNode.parentNode))" class="stretched-link"></a>

You can use the template language to create a gradually incrementing id for each card, using the i variable in your 'for' loop:

<%for(var i = 0; i<groups.length; i++){%>
<div class="col-md-4">
  <div class="card rider-card">
    <div class="card-body" id="card_<%=i%>">
      <h4 class="card-title">Text Here</h4>
      <h5 class="card-subtitle mb-2 text-muted">Text Here</h5>
      <h6 class="card-text">___ spots available</h6>
      <p class="card-text">Text Here</p>
      <a onclick="selectDriver(document.getElementById('card_<%=i%>))" class="stretched-link"></a>
    </div>
</div>
</div>
<%}%>

That's useful if you want to refer to the card in other places also. Alternately, you could bubble up and hook into the div above via parentnodes

      <a onclick="selectDriver(this.parentNode.parentNode))" class="stretched-link"></a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文