HTML DOM Figcaption 对象
JavaScript基础 2022-05-13 16:18:26小码哥的IT人生shichen
HTML DOM Figcaption 对象
Figcaption 对象
Figcaption 对象代表 HTML <figcaption> 元素。
访问 Figcaption 对象
您可使用 getElementById()
来访问 <figcaption>
元素:
示例代码:
var x = document.getElementById("myFigCap");
完整实例:
<!DOCTYPE html>
<html>
<body>
<h3>如何访问 FIGCAPTION 元素的演示</h3>
<figure>
<img src="/i/photo/tulip.jpg" alt="The Tulip" width="300" height="300">
<figcaption id="myFigCap">Fig.1 - 郁金香是百合科郁金香属的一个栽培种,又称洋荷花、旱荷花。</figcaption>
</figure>
<p>单击按钮将标题的颜色设置为红色。</p>
<button onclick="myFunction()">试一试</button>
<script>
function myFunction() {
var x = document.getElementById("myFigCap");
x.style.color = "red";
}
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
创建 Figcaption 对象
您可使用 document.createElement()
方法来创建 <figcaption>
元素:
示例代码:
var x = document.createElement("FIGCAPTION");
完整实例:
<!DOCTYPE html>
<html>
<body>
<p>单击该按钮以创建带有一些文本的 FIGCAPTION 元素。</p>
<button onclick="myFunction()">试一试</button><br><br>
<figure id="myFigure">
<img src="/i/photo/tulip.jpg" alt="The Tulip" width="300" height="300">
</figure>
<script>
function myFunction() {
var x = document.createElement("FIGCAPTION");
var t = document.createTextNode("Fig.1 - 郁金香是百合科郁金香属的一个栽培种,又称洋荷花、旱荷花。");
x.appendChild(t);
var y = document.getElementById("myFigure");
y.appendChild(x);
}
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
相关页面
HTML 参考手册:HTML <figcaption> 标签