【发布时间】:2015-08-18 13:21:53
【问题描述】:
如何设置 D3 工具提示,以便对来自变量(或只是 JSON 子项)的文本片段进行不同的样式设置?
我创建了一个工具提示,当您单击一个节点时,它会从 JSON 文件(开始上下文、单词、结束上下文)中提取文本。我想以不同的方式设置中间元素的样式(例如,使其粗体、更大等)。如果我将 .attr 或 .style 添加到变量中,我会收到“.attr 不是函数”错误。我还是很新,感谢您的任何提示!
这是相关的sn-p:
.on("click", function(d) {
tooltip.transition()
.style('opacity', 1)
var word = d.word
.style("font-size", 60 + "px");
tooltip.html(d.beginContext + " " + word + " " + d.endContext)
.style('left', 1100 + "px")
.style('top', 250 + 'px');
JSON 看起来像这样:
{"node": "",
"children": [
{
"name":"cat",
"location":141470,
"beginContext":"cried; 'canna we be quick?'\n\nBut speed was not in Mrs. Marston. She came clinging to Edward's arm, very cautiously, like a",
"word":"cat",
"endContext":"on ice.\n\nMartha, her stout red arms bare, her blue gingham dress and white apron flying in the wind, was directed to hold on to Mrs"
},
{
"name":"birds",
"location":143666,
"beginContext":"heard and blessed again. To Hazel, they seemed so many other Hazels singing because it was a festal day. To Mrs. Marston they were 'noisy",
"word":"birds",
"endContext":", and very disturbing.' Martha crotcheted. She was making edging, hundreds of yards of it, for wedding garments. This was all the more creditable"
}]}
您还可以查看小提琴here:http://jsfiddle.net/westcoast_509/ofcqq8r0/(向右滚动或缩小以查看点击时出现的文本)。
【问题讨论】:
-
你能更新小提琴,让它真正可以工作吗?
-
更新了小提琴——它只有一个 https。改为http。现在应该可以正常工作了。
标签: javascript jquery json d3.js