如何使模板已经在HTML中使用了重新出现

发布于 2025-01-20 07:13:34 字数 480 浏览 3 评论 0原文

所以我试图在它从屏幕上消失后用按钮重置它。我尝试了 document.write() 但它不起作用,所以我在使用后尝试在 HTML 中重写我的模板,这样我就可以再次将它与其他数据一起使用。这可能吗? 这是我的 HTML:

<template id="playlist__screen">
  <button class="backbutton" onclick="onClick()"></button>
  <p class="playlist__type">{{style}}</p>
  <h2 class='playlist__titre'>{{titre}}</h2>
  <p class="playlist__desc">{{description}}</p>
</template>

So I'm trying to reset it after it disappears from the screen with a button. I tried a document.write() but it doesn't work, So I try that after being used, to rewrite my template argue in my HTML so I can use it again with other datas. Is that even possible?
This is my HTML:

<template id="playlist__screen">
  <button class="backbutton" onclick="onClick()"></button>
  <p class="playlist__type">{{style}}</p>
  <h2 class='playlist__titre'>{{titre}}</h2>
  <p class="playlist__desc">{{description}}</p>
</template>

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

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

发布评论

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

评论(1

因为看清所以看轻 2025-01-27 07:13:34

以下是如何使用模板

const plays = [{
    "style": "Style 1",
    "titre": "Titre 1",
    "description": "Description 1"
  },
  {
    "style": "Style 2",
    "titre": "Titre 2",
    "description": "Description 2"
  },
  {
    "style": "Style 3",
    "titre": "Titre 3",
    "description": "Description 3"
  }
];
window.addEventListener("DOMContentLoaded", function() {
  const template = document.getElementById("template"),
    content = document.getElementById("content");
  plays.forEach(play => {
    const playList = template.content.cloneNode(true);
    playList.querySelector(".playlist__type").textContent = play.style;
    playList.querySelector(".playlist__titre").textContent = play.Titre;
    playList.querySelector(".playlist__desc").textContent = play.description;
    content.appendChild(playList);
  })
  content.addEventListener("click", function(e) {
    if (e.target.classList.contains("backbutton")) {
      console.log("back clicked");
    }
  })
});
<div id="content"></div>
<template id="template"> 
  <div class="playlist__screen">
    <button class="backbutton">Back</button>
    <p class="playlist__type"></p>
    <h2 class="playlist__titre"></h2>
    <p class="playlist__desc"></p>
  </div>
</template>

Here is how you use a template

const plays = [{
    "style": "Style 1",
    "titre": "Titre 1",
    "description": "Description 1"
  },
  {
    "style": "Style 2",
    "titre": "Titre 2",
    "description": "Description 2"
  },
  {
    "style": "Style 3",
    "titre": "Titre 3",
    "description": "Description 3"
  }
];
window.addEventListener("DOMContentLoaded", function() {
  const template = document.getElementById("template"),
    content = document.getElementById("content");
  plays.forEach(play => {
    const playList = template.content.cloneNode(true);
    playList.querySelector(".playlist__type").textContent = play.style;
    playList.querySelector(".playlist__titre").textContent = play.Titre;
    playList.querySelector(".playlist__desc").textContent = play.description;
    content.appendChild(playList);
  })
  content.addEventListener("click", function(e) {
    if (e.target.classList.contains("backbutton")) {
      console.log("back clicked");
    }
  })
});
<div id="content"></div>
<template id="template"> 
  <div class="playlist__screen">
    <button class="backbutton">Back</button>
    <p class="playlist__type"></p>
    <h2 class="playlist__titre"></h2>
    <p class="playlist__desc"></p>
  </div>
</template>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文