CSS相当于.attr("data-html","true")

key*_*int 1 html javascript css jquery d3.js

换行符继续d3圈标题tooltipText

代码是

svgContainer.selectAll("g.node").each(function() {
 var node = d3.select(this);
 var tooltipText = node.attr("name");
 var tooltipText = node.attr("name").replace("\\n", "<br />"); 
 if (tooltipText) {
   node.select("circle")
     .attr("data-html", "true")
     .attr("title", tooltipText);
 }
Run Code Online (Sandbox Code Playgroud)

我想.attr("data-html", "true")用CSS格式替换该函数

css,not-working-1:

# g.node circle { data-html: true;}
Run Code Online (Sandbox Code Playgroud)

css,not-working-2:

# g.node circle { html: true;}
Run Code Online (Sandbox Code Playgroud)

2以上都有"未知属性名称"错误.

我想知道,CSS相当于.attr("data-html", "true")什么?

非常感谢你.

小智 5

CSS是(专为)设计用于向HTML文档添加样式的简单机制.

您引用的代码实际上会改变DOM(在本例中为html节点).

旁注:如果你想根据CSS中的状态"选择",你可以这样做.

html {
  background: red;
}

html[data-something=true] {
  background: green;
}
Run Code Online (Sandbox Code Playgroud)

如果然后JavaScript设置$('html').attr('data-something', 'true');你的CSS将被应用(从红色背景到绿色).