1 回答

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
一般來說,屬性樣式會(huì)被使用選擇器的 CSS 樣式覆蓋,而 CSS 樣式又會(huì)被內(nèi)聯(lián) CSS 樣式覆蓋。
.attr("fill", "blue")
設(shè)置屬性fill="blue"
;.selected { fill: "red"; }
應(yīng)用風(fēng)格fill: red;
;.style("fill", "green")
設(shè)置屬性style="fill: green;"
,內(nèi)聯(lián)樣式。
如果將所有這些應(yīng)用于同一元素,則最后一個(gè)優(yōu)先。通過取消選擇先前選擇的欄,您可以賦予它.style("fill"
,從而覆蓋任何潛在的未來樣式。我建議檢查您是否真的需要應(yīng)用這些樣式,.attr()
如果需要就使用。
我在下面添加了一個(gè)小展示柜。
let settings = {
attr: false,
class: false,
style: false,
};
draw();
function draw() {
d3.select("rect")
.attr("fill", settings.attr ? "red" : null)
.attr("class", settings.class ? "blue" : "")
.style("fill", settings.style ? "green" : null);
}
d3.selectAll("[type='checkbox']")
.on("change", function() {
settings[this.getAttribute("id")] = this.checked;
draw();
});
.blue {
fill: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div>
<input type="checkbox" id="attr"/><label for="attr">attr</label>
<input type="checkbox" id="class"/><label for="class">class</label>
<input type="checkbox" id="style"/><label for="style">style</label>
</div>
<svg>
<rect width="30" height="30"></rect>
</svg>
添加回答
舉報(bào)