selection.html()函數用於在所有選定元素上設置內部HTML。如果該值是常數,則所有元素都被賦予相同的值。空值將清除元素的內容。
用法:
selection.html([value]);
參數:該函數接受上述和以下描述的單個參數:
- Value:它是將HTML設置為文檔的字符串類型。
返回值:此函數不返回任何內容。
範例1:在以下代碼中,HTML “p”元素內容更改為“bold”文本。
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>
<style>
p {
background-color:#f2f2f2;
padding:10px;
width:200px;
line-height:5px;
}
p:hover {
background-color:grey;
padding:10px;
cursor:pointer;
}
</style>
</head>
<body>
<div>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h4>D3.js selection.html() Function</h4>
<p>Click Here!</p>
</div>
<script>
function func() {
// Selecting all p and setting
// the innerHTML of the p
var chk = d3.selectAll("p")
.html("<b>This is from .html</b>");
var text = document.querySelector("p");
}
let btn = document.querySelector("p");
btn.addEventListener("click", func);
</script>
</body>
</html>
輸出:
範例2:下麵的示例使用null清除內容。
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>
<style>
p {
background-color:#f2f2f2;
padding:10px;
width:200px;
line-height:5px;
}
p:hover {
background-color:grey;
padding:10px;
cursor:pointer;
}
</style>
</head>
<body>
<div>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h4>D3.js selection.html() Function</h4>
<p><b>Click Here</b></p>
</div>
<script>
function func() {
// Selecting p and setting the
// innerHTML of the p
var chk = d3.selectAll("p")
.html(null);
var text = document.querySelector("p");
}
let btn = document.querySelector("p");
btn.addEventListener("click", func);
</script>
</body>
</html>
輸出:
相關用法
- d3.js now()用法及代碼示例
- p5.js mag()用法及代碼示例
- PHP next()用法及代碼示例
- d3.js zip()用法及代碼示例
- d3.js lch()用法及代碼示例
- d3.js tsv()用法及代碼示例
- p5.js pan()用法及代碼示例
- CSS var()用法及代碼示例
- PHP Ds\Map get()用法及代碼示例
- PHP chr()用法及代碼示例
- PHP ord()用法及代碼示例
- CSS hsl()用法及代碼示例
- p5.js min()用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js selection.html() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。