简体   繁体   English

如何将 class(基于 if - else 的结果)动态分配给使用 Bootstrap Tagsinput 的弹出工具提示的 span 标签

[英]How to dynamically assign a class (based on the outcome of if - else) to a span tag of a popover tooltip that uses Bootstrap Tagsinput

//My js file has the following //我的js文件有以下内容

$mytag.popover({
html:true,
trigger: hover,
container: $mytag,
template: '<div class="popover" role="showDetails">' +
          ...//other
           '</div>',
content:'<div class="info">' +
          ...//other
          '<div class="myrow">' +
          '<span>' + 'Result:'  + '</span>' +
           '<span>' + item.result + '</span>' +
          + '</div>' + 
}); 

The value of item.result is calculated elsewhere (js). item.result 的值在别处(js)计算。 The final outcome is expected to be a Boolean value.最终结果预计为 Boolean 值。 I'd like to append a css class here instead of displaying the outcome.我想在这里 append 一个 css class 而不是显示结果。 Eg: If item.result is true.例如:如果 item.result 为真。 I'd like to add class="ok" to the span tag.我想将 class="ok" 添加到 span 标签。

'<span class="ok">' + '</span>' +

If item.result returns false.如果 item.result 返回 false。 I'd like to add class="notOk" to the span tag.我想将 class="notOk" 添加到 span 标签。

'<span class="notOk">' + '</span>' +

Can someone please advise what is the best way to achieve this?.有人可以建议实现这一目标的最佳方法是什么?

Thanks in advance.提前致谢。

you can achieve this with string interpolation.您可以通过字符串插值来实现这一点。

`<span class="${calcutationFn() ? 'ok' : 'noOK'}"> ... </span>`

Have you tried adding an id to the span and then using the element.classlist.add('class_name') function?您是否尝试过向span添加id ,然后使用element.classlist.add('class_name') function? Add an if statement where the item.result is calculated to add the class to the span like:添加一个 if 语句,其中计算item.result以将 class 添加到跨度中,例如:

if (item.result) {
    document.getElementById("result-span").classList.add("ok"); 
}
else {
    document.getElementById("result-span").classList.add("notOk");
}

HTML DOM classList Property HTML DOM classList 属性

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

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