HTML DOM source 对象
htmlweb developmentfront end technology更新于 2024/5/28 3:24:17
HTML DOM source 对象与 HTML <source> 元素关联。我们可以分别使用 createElement() 和 getElementById() 方法创建和访问源元素。
语法
以下是语法 −
创建源对象 −
var p= document.createElement("SOURCE");
示例
我们来看一个源对象的示例 −
<!DOCTYPE html>
<html>
<body>
<h1>Source object example</h1>
<audio id="AUDIO_1" controls >
</audio>
<p>Add a audio source for the above element by clicking the below element.</p>
<button onclick="createSrc()">CREATE</button>
<p id="Sample"></p>
<script>
function createSrc() {
var s = document.createElement("SOURCE");
s.setAttribute("src", "sample.mp3");
s.setAttribute("type", "audio/mpeg");
document.getElementById("AUDIO_1").appendChild(s);
document.getElementById("Sample").innerHTML="The audio control will work now";
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击"创建"按钮
点击"创建" −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

