如果您尝试在浏览器中打开音频文件,浏览器将创建以下代码:
<video controls autoplay name="media">
<source src="https://stopmusic.net/_ld/65/6549_B.B.E-Seven_Day.mp3" type="audio/mp3">
</video>
他为什么使用 <video>
创建标记标签?
如果我们看一下 specification ,我们将看到以下内容:
When an image, video, or audio resource is to be loaded in a browsing context, the user agent should create a Document object, mark it as being an HTML document, set its content type to the computed MIME type of the resource (type in the navigate algorithm), initialize the Document object, append an html element to the Document, append a head element and a body element to the html element, append an element host element for the media, as described below, to the body element, and set the appropriate attribute of the element host element, as described below, to the address of the image, video, or audio resource.
正如您在浏览器中打开特定媒体文件时所看到的,浏览器会创建与其关联的特定代码。在这种情况下,由于某种原因,浏览器不会创建与之关联的代码。为什么会这样?这是实现规范错误吗?
最佳答案
我假设这是谷歌浏览器自主完成的事情,我觉得他们有自己的专有算法,将文件类型等同于特定标签,如 <video>
, <audio>
和 <img>
.我也会认为 .mp3
文件将由 <audio>
打开标签,没有别的,因为它是一个音频文件。在考虑了这些事情之后,这个特性将执行它在事物的生产端所做的事情是没有逻辑意义的。人们会假设任何以“mp4”扩展名结尾的文件都使用 <video>
标签。我认为这是他们正在完成并决定无论如何都要推出的一项功能的主要示例,因为它有效并且不需要任何更多的努力。
关于html - 为什么浏览器不创建相应的 html 代码来打开音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53790714/