--- title: MediaList slug: Web/API/MediaList tags: - API - CSSOM - Interface - MediaList - Reference translation_of: Web/API/MediaList --- <div>{{APIRef("CSSOM")}}</div> <p><code><strong>MediaList</strong></code> 接口表示样式表的媒体查询,例如使用了 <code>media</code> 属性的{{htmlelement("link")}} 元素。</p> <div class="blockIndicator note"> <p><span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox objectBox-string"><strong>注意:</strong><code>MediaList</code> 是一个实时列表;使用以下属性或方法更新列表会立刻更新文档的表现。</span></span></span></span></p> </div> <h2 id="属性">属性</h2> <dl> <dt>{{domxref("MediaList.mediaText")}}</dt> <dd>一个字符串转化器,返回一个{{domxref("DOMString")}},以文本形式表示 <code>MediaList</code>,也可以通过这个方法设置新的 <code>MediaList</code>。</dd> <dt>{{domxref("MediaList.length")}} {{readonlyInline}}</dt> <dd>返回 <code>MediaList</code> 中媒体查询的数量。</dd> </dl> <h2 id="方法">方法</h2> <dl> <dt>{{domxref("MediaList.appendMedium()")}}</dt> <dd>向 <code>MediaList</code> 中添加一个媒体查询。</dd> <dt>{{domxref("MediaList.deleteMedium()")}}</dt> <dd>从 <code>MediaList</code> 中移除一个媒体查询。</dd> <dt>{{domxref("MediaList.item()")}}</dt> <dd>一个获取函数,返回一个{{domxref("CSSOMString")}},表示文本形式的媒体查询,需要提供媒体查询在 <code>MediaList</code> 中的索引位置。</dd> </dl> <h2 id="示例">示例</h2> <p>下述例子会在控制台记录 <code>MediaList</code> 的文本表示,其来自应用到当前文档的第一个样式表。</p> <pre class="brush: js">const stylesheets = document.styleSheets; let stylesheet = stylesheets[0]; console.log(stylesheet.media.mediaText);</pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">规范</th> <th scope="col">状态</th> <th scope="col">注释</th> </tr> <tr> <td>{{SpecName('CSSOM', '#the-medialist-interface', 'MediaList')}}</td> <td>{{Spec2('CSSOM')}}</td> <td></td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("api.MediaList")}}</p>