使用jQuery在元素之后插入HTML

时间:2019-01-29 22:10:53

标签: javascript jquery insertafter

我希望在现有页面上的特定元素之后添加一个按钮。这是我到目前为止尝试过的代码。

$('.collection-nav').after('<span class="button">This is a button.</span>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<nav class="index-navigation collection-nav" role="navigation">


  <div class="collection-nav-item" data-url-id="process">
    <a href="/process/" class="process">
      <span class="collection-nav-item-span">Process</span>
    </a>
  </div>


  <div class="collection-nav-item" data-url-id="our-breads">
    <a href="/our-breads/" class="our-breads">
      <span class="collection-nav-item-span">Bread</span>
    </a>
  </div>


  <div class="collection-nav-item" data-url-id="ourculutre">
    <a href="/ourculutre/" class="ourculutre">
      <span class="collection-nav-item-span">Culture</span>
    </a>
  </div>


  <div class="collection-nav-item" data-url-id="about">
    <a href="/about/" class="about">
      <span class="collection-nav-item-span">About</span>
    </a>
  </div>


</nav>

我这里想念什么吗?

1 个答案:

答案 0 :(得分:-1)

据我所知,您编写的代码是正确的,但需要触发该代码才能工作。

您可以添加这样的按钮=> Activity

您可以稍后再试您的代码:

<button id="btnAfter">After Element</button>