d3.selector()函數用於返回一個函數,該函數返回作為參數給出的元素的第一個後代。
用法:
d3.selector(selector)
參數:此函數僅接受上麵給出並在下麵描述的一個參數:
- selector:這是要選擇的元素的字符串。
返回值:該函數返回一個布爾值。
下麵給出的是上麵給出的函數的一些例子。
範例1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
path1tent="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<style>
p{
line-height:5px;
}
</style>
<body>
<div>
</div>
<div>
<p>This is para1</p>
<p>This is para2</p>
<p>This is para3</p>
<p>This is para4</p>
</div>
<script src =
"https://d3js.org/d3.v4.min.js">
</script>
<script>
// Parent of div is HTML
var div = d3.select("div").select(d3.selector("div"));
console.log(div);
</script>
</body>
</html>
輸出:
範例2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
path1tent="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<style>
p{
line-height:5px;
}
</style>
<body>
<div>
<p>This is para1
<p>This is para2</p>
<p>This is para3</p>
</p>
</div>
<script src =
"https://d3js.org/d3.v4.min.js">
</script>
<script>
// First descendant of Div
var para = d3.select("div").select(d3.selector("p"));
console.log(para);
// First descendant of Div
var p = d3.select("div").select("p");
console.log(p);
console.log("Both p and para are equal");
console.log(para.node());
console.log(p.node());
</script>
</body>
</html>
輸出:
相關用法
- jQuery :input用法及代碼示例
- jQuery :nth-of-type()用法及代碼示例
- jQuery :enabled用法及代碼示例
- jQuery :last用法及代碼示例
- jQuery :button用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js selector() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。