d3.creator()函數用於返回一個函數,該函數創建一個元素,該元素的名稱作為函數中的參數給出。
用法:
d3.creator( name );
參數:該函數接受上述和以下描述的單個參數:
- name:它是要創建的容器或HTML標記的名稱。
返回值:它返回一個函數。
範例1:在此示例中,在主體內添加div元素。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" path1tent=
"width=device-width,initial-scale=1.0">
<script src=
"https://d3js.org/d3.v4.min.js">
</script>
<script src=
"https://d3js.org/d3-selection.v1.min.js">
</script>
<style>
div {
background-color:green;
color:honeydew;
width:fit-content;
padding:10px;
}
</style>
</head>
<body>
<!-- No div tag is added here -->
<script>
let selection = d3.select("body")
// Creating and appending
// a div to the body
selection.append(d3.creator("div"));
let div = document.querySelector("div")
div.innerText =
"Div tag created using d3.creator()"
</script>
</body>
</html>
輸出:
範例2:在此示例中,將多個標簽附加到主體。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" path1tent=
"width=device-width,initial-scale=1.0">
<script src=
"https://d3js.org/d3.v4.min.js">
</script>
<script src=
"https://d3js.org/d3-selection.v1.min.js">
</script>
<style>
div {
background-color:green;
color:honeydew;
width:fit-content;
padding:10px;
height:100px;
}
</style>
</head>
<body>
<!-- No div tag is added here -->
<script>
var selection = d3.select("body")
// Creating and appending a
// div to the body
selection.append(d3.creator("div"));
var selection = d3.select("body")
// Creating and appending a p
// tag to the div
selection.append(d3.creator("p"));
// Creating and appending a button
// tag to the div
selection.append(d3.creator("button"));
var div = document.querySelector("div")
div.innerHTML =
"Div tag created using d3.creator()"
var div = document.querySelector("p")
div.innerHTML =
"paragraph tag created using d3.creator()"
var div = document.querySelector("button")
div.innerHTML =
"paragraph tag created using d3.creator()"
</script>
</body>
</html>
輸出:
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP array_udiff_uassoc()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP cal_to_jd()用法及代碼示例
- d3.js d3.bisectLeft()用法及代碼示例
- PHP stream_get_transports()用法及代碼示例
- PHP Ds\Deque pop()用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js creator() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。