重新初始化Ajax调用后的引导轮播。

huangapple go评论109阅读模式
英文:

Reinitialize bootstrap carousel after Ajax call

问题

I can provide the translated code portion for your request:

  1. 在进行了 `Ajax 调用` 我正在设置 `bootstrap 轮播` DOM 所以我在页面加载后附加元素和类如下所示
  2. ```javascript
  3. async function ajaxCall() {
  4. let data = await fetch('ApiLink');
  5. let jsonData = await data.json();
  6. let carousel = document.createElement('section');
  7. let inner = document.createElement('div');
  8. carousel.id = 'slider';
  9. carousel.className = 'carousel carousel-dark slide';
  10. carousel.setAttribute('data-bs-ride', 'carousel');
  11. inner.className = 'carousel-inner';
  12. jsonData.data.forEach(data => {
  13. let item = document.createElement('div');
  14. let avatar = document.createElement('img');
  15. item.className = 'carousel-item';
  16. avatar.src = data.avatar;
  17. item.appendChild(avatar);
  18. inner.appendChild(item);
  19. });
  20. inner.querySelector('.carousel-item').classList.add('active');
  21. carousel.appendChild(inner);
  22. document.body.appendChild(carousel);
  23. }

然而,轮播不起作用。

我猜想,我需要在创建和附加 HTML 元素后重新初始化轮播。所以,我的问题是:

如何在 Ajax 调用后重新初始化 bootstrap 轮播,而不使用 jQuery

  1. 请注意,这是原始代码的翻译,如果需要进一步的帮助或解释,请随时告诉我。
  2. <details>
  3. <summary>英文:</summary>
  4. I&#39;m setting up the `bootstrap carousel` after the `Ajax call` to the DOM. So, I&#39;m appending elements and classes `after the page load` as below.

async function ajaxCall() {
let data = await fetch('ApiLink');
let jsonData = await data.json();

  1. let carousel = document.createElement(&#39;section&#39;);
  2. let inner = document.createElement(&#39;div&#39;);
  3. carousel.id = &#39;slider&#39;;
  4. carousel.className = &#39;carousel carousel-dark slide&#39;;
  5. carousel.setAttribute(&#39;data-bs-ride&#39;, &#39;carousel&#39;);
  6. inner.className = &#39;carousel-inner&#39;;
  7. jsonData.data.forEach(data =&gt; {
  8. let item = document.createElement(&#39;div&#39;);
  9. let avatar = document.createElement(&#39;img&#39;);
  10. item.className = &#39;carousel-item&#39;;
  11. avatar.src = data.avatar;
  12. item.appendChild(avatar);
  13. inner.appendChild(item);
  14. });
  15. inner.querySelector(&#39;.carousel-item&#39;).classList.add(&#39;active&#39;);
  16. carousel.appendChild(inner);
  17. document.body.appendChild(carousel);

}

  1. However, the carousel doesn&#39;t work.
  2. I guess, I need to reinitialize the carousel after creating and appending the HTML elements. So, my question is:
  3. How can I reinitialize the bootstrap carousel after the Ajax call **without using jQuery**?
  4. </details>
  5. # 答案1
  6. **得分**: 1
  7. 根据[文档][1],在创建您的滑块元素后,您可以使用以下代码创建一个轮播实例:
  8. let carousel = new bootstrap.Carousel(slider, {});
  9. 此外,请查看轮播方法。因此,您可以找到`getOrCreateInstance`,它返回与DOM元素关联的轮播实例,或者在未初始化时创建一个如下:
  10. bootstrap.Carousel.getOrCreateInstance(slider, {});
  11. 顺便说一下,您可以将任何属性作为第二个参数传递给您的滑块。
  12. 另外,`data-bs-ride`属性在页面加载时仅读取一次。因此,在Ajax调用中无需设置它。
  13. [1]: https://getbootstrap.com/docs/5.1/components/carousel/#methods
  14. <details>
  15. <summary>英文:</summary>
  16. Based on the [documentation][1], you can make a carousel instance with the carousel constructor after creating your slider element as below:
  17. let carousel = new bootstrap.Carousel(slider, {});
  18. Further, take a look at the carousel methods. So, you can find the `getOrCreateInstance` which returns a carousel instance associated with a DOM element, or create a new one in case it wasn&#39;t initialized as below:
  19. bootstrap.Carousel.getOrCreateInstance(slider, {});
  20. FYI, you can pass any attributes to your slider as the second parameter.
  21. Also, the `data-bs-ride` attribute reads once on page load. So, you don&#39;t need to set it up in your Ajax call.
  22. [1]: https://getbootstrap.com/docs/5.1/components/carousel/#methods
  23. </details>

huangapple
  • 本文由 发表于 2023年6月5日 00:49:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定