我正在使用 svg 元素
var defsElement = document.querySelector('defs');
var defsElementX = defsElement.getBBox().x;
var defsElementY = defsElement.getBBox().y;
var colX = [];
for (var i = 0; i < 3; i ) {
(i == 0) ? colX.push(25): colX.push(colX[i - 1] 60)
};
//console.log(colX);
const y = 145;
const svg = document.querySelector("svg");
const svgns = "http://www.w3.org/2000/svg"
colX.forEach(
(a, i) => {
let useEl = document.createElementNS(svgns, 'use');
useEl.setAttributeNS('http://www.w3.org/1999/xlink', "xlink:href", "#stick man");
useEl.setAttribute("class", "use" [i]);
useEl.setAttribute("id", "id" [i]);
useEl.setAttribute("x", `${a}`);
useEl.setAttribute("y", `${y}`);
svg.appendChild(useEl);
}
)
.stickman {
stroke: red;
fill: none;
}
.stickman>.head {
fill: black;
stroke: blue;
}
.head {
fill: brown;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<link rel="stylesheet" href="style.css"></link>
<svg id="layer" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">
<defs>
<g id="stick man" style="fill: none; stroke: black;">
<circle class="head" cx="10" cy="10" r="10"/>
<line class="body" x1="10" y1="20" x2="10" y2="44"/>
<polyline class="upper" points="1 58, 10 44, 19 58"/>
<polyline class="lower" points="1 24, 10 30, 19 24"/>
</g>
</defs>
<g class="stickman" id="stickman" transform = "translate (85,50)">
<circle class="head" cx="10" cy="10" r="10"/>
<line class="body" x1="10" y1="20" x2="10" y2="44"/>
<polyline class="upper" points="1 58, 10 44, 19 58"/>
<polyline class="lower" points="1 24, 10 30, 19 24"/>
<script href="index.js"></script>
</svg>
</body>
</html>
我想學習如何將 CSS 樣式應用于不同的<use></use>元素。例如,如何fill為每個head. 我應該使用什么 CSS 選擇器?
我試過了
.use0>.head {
fill: brown;
}
它沒有做任何事情。
javascript正在生成這個

uj5u.com熱心網友回復:
您可以為此使用 CSS 變數:
const svg = document.querySelector("svg");
const svgns = "http://www.w3.org/2000/svg";
for (let i = 0; i < 3; i) {
let useEl = document.createElementNS(svgns, 'use');
useEl.setAttributeNS('http://www.w3.org/1999/xlink', "xlink:href", "#stickRef");
useEl.setAttribute("id", `id${i}`);
useEl.setAttribute("x", (i * 60) 25);
useEl.setAttribute("y", 25);
svg.appendChild(useEl);
}
#id0 { --head-colour: #F00; --line-colour: #900; }
#id1 { --head-colour: #0F0; --line-colour: #090; }
#id2 { --head-colour: #00F; --line-colour: #009; }
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">
<defs>
<symbol id="stickRef" style="fill: none; stroke: var(--line-colour);">
<circle class="head" cx="10" cy="10" r="10" style="fill: var(--head-colour)" />
<line class="body" x1="10" y1="20" x2="10" y2="44"/>
<polyline class="upper" points="1 58, 10 44, 19 58"/>
<polyline class="lower" points="1 24, 10 30, 19 24"/>
</symbol>
</defs>
</svg>
uj5u.com熱心網友回復:
您可以僅使用火柴人的直線和多段線將其包裹在一個組<g>中style="fill: none;",從而使圓圈具有未指定的填充。
<use>現在您可以為元素設定填充屬性或賦予樣式。填充將僅應用于圓。
use{fill:red}
use:nth-of-type(2){fill:gold}
use:nth-of-type(3){fill:blue}
<svg id="layer" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 100 500 500">
<defs>
<g id="stickman" style="stroke: black;">
<circle class="head" cx="10" cy="10" r="10"></circle>
<g style="fill: none;">
<line class="body" x1="10" y1="20" x2="10" y2="44"></line>
<polyline class="upper" points="1 58, 10 44, 19 58"></polyline>
<polyline class="lower" points="1 24, 10 30, 19 24"></polyline>
</g>
</g>
</defs>
<use xlink:href="#stickman" x="25" y="145"></use>
<use xlink:href="#stickman" x="85" y="145"></use>
<use xlink:href="#stickman" x="145" y="145"></use>
</svg>
uj5u.com熱心網友回復:
在你的例子中沒有use0類,所以顯然沒有改變任何東西和你的規則:
.head {
fill: brown;
}
只是被以下內容覆寫:
.stickman>.head {
fill: black;
}
確保你有一個優先的 css 規則來覆寫另一個規則......如果可能的話,有很多方法可以避免,!Important例如:
body .stickman .head {
fill: brown;
}
更多關于 CSS 優先級的資訊在這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/441111.html
標籤:javascript css svg css 选择器
