有CSS代码 定义了一些一个叫data-classbreak的属性,取不同属性值时的style效果:
path[data-classbreak="classbreak0"] {
stroke: rgb(255, 245, 220);
stroke-width: 1pt;
stroke-opacity: 0.35;
fill: #00008B;
fill-opacity: 0.8;
}
path[data-classbreak="classbreak1"] {
stroke: rgb(255 ,140 ,0);
stroke-width: 1pt;
stroke-opacity: 0.35;
fill: #1E90FF;
fill-opacity: 0.8;
}
path[data-classbreak="classbreak2"] {
stroke: rgb(255, 245, 220);
stroke-width: 1pt;
stroke-opacity: 0.35;
fill: #00008B;
fill-opacity: 0.8;
}
源代码中有一句, node.setAttribute("data-classbreak", “classbreak0”);
即为一个节点的data-classbreak赋予classbreak0对应的style。
现在我需要用jquey动态地修改例如path[data-classbreak="classbreak0"]中的fill属性,尝试 $("path[data-classbreak='classbreak0]").css("fill","#33FF00")并没有效果,请问应该怎么改?谢谢!
$(".path[data-classbreak='classbreak0]").css("fill","#33FF00"); //注意前面的“.”,表示样式,而单纯的path的话就表示path元素,而你的node里面并没有path这样的元素,肯定找不到的。汗!!!
fill是样式属性$("path[data-classbreak='classbreak0]").style("fill","#33FF00")你试过这样么
$('path[data-classbreak="classbreak1"]').attr("data-classbreak","classbreak0");
前面为什么有个path呢?
[data-classbreak="classbreak0"] {
stroke: rgb(255, 245, 220);
stroke-width: 1pt;
stroke-opacity: 0.35;
fill: #00008B;
fill-opacity: 0.8;
}
[data-classbreak="classbreak1"] {
stroke: rgb(255 ,140 ,0);
stroke-width: 1pt;
stroke-opacity: 0.35;
fill: #1E90FF;
fill-opacity: 0.8;
}
[data-classbreak="classbreak2"] {
stroke: rgb(255, 245, 220);
stroke-width: 1pt;
stroke-opacity: 0.35;
fill: #00008B;
fill-opacity: 0.8;
}
jQuery("[data-classbreak='classbreak0']").css("fill","#33FF00");
这样子就可以啊
path是svg对象,fill是属性,不是css,设置css没用,用attr
$('path[data-classbreak="classbreak1"]').attr('fill','red')