Đối tượng nguồn HTML DOM được liên kết với phần tử HTML
Cú pháp
Sau đây là cú pháp cho -
Tạo đối tượng nguồn -
var p= document.createElement("SOURCE"); Ví dụ
Chúng ta hãy xem một ví dụ cho đối tượng nguồn -
<!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> Đầu ra
Điều này sẽ tạo ra kết quả sau -
Khi nhấp vào nút TẠO -
Nhấp vào “TẠO” -