HTML DOM 快速導覽 - 元素物件 <source> (HTMLSourceElement)

<source> 為 HTML 5 的內嵌元素 (embedded content) , DOM 介面為 HTMLSourceElement 。



取得 <source> 的元素物件 (object) 後,可存取元素的全域屬性 (global attribute) 。 HTMLSourceElement 另有定義屬性
  • src
  • type
  • media


srctypemedia 對應到 <source> 相同名稱的屬性。舉例如下
function run() {
    var d = document.getElementsByTagName("source");
    var s = document.getElementById("show");
    s.innerHTML = d[0];
}

/* 《程式語言教學誌》的範例程式
     http://pydoing.blogspot.com/
     檔名:htmldemo070.js
     功能:示範 JavaScript 程式 
     作者:張凱慶
     時間:西元 2011 年 8 月 */


此例用 document 物件的 getElementsByTagName() 方法 (method) ,取得標籤名稱為 audio 的 HTMLCollection 物件,文件的第一個 <audio> 元素亦即該 HTMLCollection 索引值為 0 的物件
var d = document.getElementsByTagName("source");
var s = document.getElementById("show");
s.innerHTML = d[0];


然後在 id 屬性 (attribute) 為 show 的元素中顯示 d[0] 。


我們以下面的 HTML 文件載入
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM DEMO</title>
<script src="htmldemo070.js" type="text/javascript"></script>
</head>
<body>
<div>
<audio autoplay controls>
<source src="example1.mp3">
</audio>
</div>
<input type="button" value="RUN" onclick="run();">
<div>Result: <span id="show"></span></div>
</html>

<!-- 《程式語言教學誌》的範例程式
     http://pydoing.blogspot.com/
     檔名:htmldemo070.html
     功能:示範 JavaScript 程式 
     作者:張凱慶
     時間:西元 2011 年 8 月 -->


瀏覽器 (broswer) 開啟,按 RUN 後如下



中英文術語對照
內嵌元素embedded content
物件object
全域屬性global attribute
方法method
屬性attribute
瀏覽器broswer


您可以繼續參考
HTML 5 範例

HTML 元素物件


相關目錄
HTML DOM 快速導覽
JavaScript 教材
首頁


參考資料
https://developer.mozilla.org/en/DOM/HTMLSourceElement
https://developer.mozilla.org/en/HTML/Element/source
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#the-source-element

沒有留言: