如何在函数内追加 html 元素
How to append html element inside a function
这是我从函数中获得的代码:
.text(function (d, i) { return data[i].label + " <tspan style='font-weight:700;'>(" + data[i].value + ")</tspan>" });
但它 output
MARCO <tspan style='font-weight:700;'>(7)</tspan>
而不是 MARCO <tspan style='font-weight:700;'>(7)</tspan>
。
所以它不是评估为HTML。我应该在哪里解码 "inject" 之前的文本?
使用 html()
而不是 text()
。 text()
将对字符串进行编码并将其添加到 DOM.
.html(function (d, i) {
return data[i].label + " <tspan style='font-weight:700;'>(" + data[i].value + ")</tspan>"
});
这是我从函数中获得的代码:
.text(function (d, i) { return data[i].label + " <tspan style='font-weight:700;'>(" + data[i].value + ")</tspan>" });
但它 output
MARCO <tspan style='font-weight:700;'>(7)</tspan>
而不是 MARCO <tspan style='font-weight:700;'>(7)</tspan>
。
所以它不是评估为HTML。我应该在哪里解码 "inject" 之前的文本?
使用 html()
而不是 text()
。 text()
将对字符串进行编码并将其添加到 DOM.
.html(function (d, i) {
return data[i].label + " <tspan style='font-weight:700;'>(" + data[i].value + ")</tspan>"
});