简体   繁体   English

如何在 Handlebars 模板中动态添加类

[英]How to dynamically add class in Handlebars template

I am trying to either dynamically create HTML elements in my Handlebars template, specifically:我正在尝试在我的 Handlebars 模板中动态创建 HTML 元素,特别是:

<span class="fa fa-star checked"></span>

Or I want to be able to select existing elements and add the 'checked' class to them.或者我希望能够选择现有元素并向它们添加“已检查”类。 It's for a star rating display, if a book has a rating of 5, then I want 5 out of 5 spans to have the checked class and styled appropriately.这是用于星级显示,如果一本书的评分为 5,那么我希望 5 个跨度中有 5 个具有选中的类并进行适当的样式设置。

So far I've tried using jQuery to target the span elements and add a class ie.到目前为止,我已经尝试使用 jQuery 来定位 span 元素并添加一个类,即。 $('#star-1').addClass('checked'); But I get a null value when searching for that element.但是在搜索该元素时我得到了一个空值。

I've also tried to append child elements to a parent div with a for loop, but I get a similar problem with not being able to find and select the parent container.我还尝试使用 for 循环将子元素附加到父 div,但我遇到了类似的问题,无法找到和选择父容器。 The closest I've got is shown below with returning a single span element in a for loop or returning 5 span elements without the class.我得到的最接近的如下所示,在 for 循环中返回单个 span 元素或在没有类的情况下返回 5 个 span 元素。 Any help would be appreciated.任何帮助,将不胜感激。

In my template.html file:在我的 template.html 文件中:

<div class="rating-container">
    {{#renderStars rating}}
    <span class="fa fa-star" id="star-1"></span>
    <span class="fa fa-star" id="star-2"></span>
    <span class="fa fa-star" id="star-3"></span>
    <span class="fa fa-star" id="star-4"></span>
    <span class="fa fa-star" id="star-5"></span>
    {{/renderStars}}
</div>

Then in my app.js file:然后在我的 app.js 文件中:

Handlebars.registerHelper('renderStars', (rating) => {
  for (let i = 0; i <= rating - 1; i++) {
    return new Handlebars.SafeString("<span class='fa fa-star checked'></span>");
  };
});

Finally a snippet of the context JSON data being passed to Handlebars template:最后是传递给 Handlebars 模板的上下文 JSON 数据片段:

[
  {
    "month": "January",
    "image": "/images/sapiens.jpg",
    "altText": "Sapiens: A Brief History of Humankind, book by Yuval Noah Harari",
    "title": "Sapiens: A Brief History of Humankind",
    "author": "Yuval Noah Harari",
    "rating": 5,
},

"I've tried using jQuery [...] But I get a null value when searching for that element." “我尝试过使用 jQuery [...] 但是在搜索该元素时我得到了一个空值。”

You need to wait until the stars are appended to the DOM, before doing that.在此之前,您需要等到星星被附加到 DOM 中。

Regarding your for loop, it will only execute one iteration, since you return (end the function) during the first iteration.关于您的for循环,它只会执行一次迭代,因为您在第一次迭代期间return (结束函数)。

You could try this:你可以试试这个:

Handlebars.registerHelper('renderStars', (rating) => {
  let result = '';
  for (let i = 1; i <= 5; i++) {
    let checked = rating >= i ? ' checked' : '';
    result += `<span class='fa fa-star${checked}'></span>`;
  }
  return new Handlebars.SafeString(result);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM