Hi
World
select 選我1
select 選我2
select 選我3
select 選我4
select 選我5
程式碼
// html
<div class="select">
<p>select 選我1</p>
<p>select 選我2</p>
<p>select 選我3</p>
<p>select 選我4</p>
<p>select 選我5</p>
</div>
//JS
// 參數帶字串
d3.select('.select p').style('color', 'red');
// 參數帶 node 節點
const node = document.querySelector(".select p");
d3.select(node).style("color", "red");
selectAll 選我1
selectAll 選我2
selectAll 選我3
selectAll 選我4
selectAll 選我5
程式碼
// html
<div class="selectAll">
<p>selectAll 選我1</p>
<p>selectAll 選我2</p>
<p>selectAll 選我3</p>
<p>selectAll 選我4</p>
<p>selectAll 選我5</p>
</div>
//selectAll
// 帶入字串
d3.selectAll(".selectAll p").style("color", "red");
// 或是帶入 node 節點陣列
const nodeArray = document.querySelectorAll(".selectAll p");
d3.selectAll(nodeArray).style("color", "red");
程式碼
//html
<svg width="500" height="200" class="svgCircleWrap-attr"
style="border: 1px solid rgb(96, 96, 96)">
<circle></circle>
</svg>
//JS
d3.select('.svgCircleWrap-attr circle')
.attr("r", 30)
.attr("cx", 50)
.attr("cy", 50)
.attr("fill", "rgb(248, 204, 61)")
程式碼
//html
<svg width="500" height="200" class="svgCircleChangePositionWrap "
style="border: 1px solid rgb(96, 96, 96)">
<circle></circle>
</svg>
//JS
d3.select('.svgCircleChangePositionWrap circle')
.attr("r", 30),
.attr("cx", 50),
.attr("cy", 50),
.attr("fill", "rgb(248, 204, 61)"),
.attr("transform", "translate(400,100)");
程式碼
//html
<svg width="500" height="200"
class="svgCircleWrap-classed "
style="border: 1px solid rgb(96, 96, 96)">
<circle></circle>
</svg>
//CSS
.circle {
r: 30px;
cx: 50px;
cy: 50px;
}
.yellow {
fill: rgb(248, 204, 61);
}
//JS
d3.select(".svgCircleWrap-classed circle")
.classed("yellow circle", true);
程式碼
//html
<svg width="500" height="200"
class="svgCircleWrap-style "
style="border: 1px solid rgb(96, 96, 96)">
<circle></circle>
</svg>
//JS
d3.select(".svgCircleWrap-style circle")
.style("r", "30px")
.style("cx", "50px")
.style("cy", "50px")
.style("fill", "rgb(248, 204, 61)", "important");
Hi
World
程式碼
//html
<div class="text">
<p>Hi</p>
<p>World</p>
</div>
//JS
d3.select(".text")
.text("Hello World")
a
b
c
d
程式碼
//html
<div class="append">
<p>a</p>
<p>b</p>
<p>c</p>
<p>d</p>
</div>
//JS
d3.select('.append')
.append('p')
.text('e')
.classed('fw-bold',true)
.style('color','rgb(248, 117, 61)')
a
b
c
d
程式碼
//html
<div class="insert">
<p>a</p>
<p>b</p>
<p>c</p>
<p>d</p>
</div>
//JS
d3.select(".insert")
.insert("p", "p:nth-child(2)")
.text("e")
.classed("fw-bold", true)
.style("color", "rgb(248, 117, 61)");
a
b
c
d
程式碼
//html
<div>
<p>a</p>
<p class="remove">b</p>
<p>c</p>
<p>d</p>
</div>
//JS
d3.select('.remove')
.remove()
我是一個p