亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

修改視頻元素的outerHTML而不破壞視頻

修改視頻元素的outerHTML而不破壞視頻

慕哥9229398 2023-10-10 15:01:24
我正在開發一個 Firefox 擴展,用于向視頻元素添加疊加層。我使用https://www.w3schools.com/html/mov_bbb.mp4作為一個單獨的示例進行測試。但是,如果我說:$0.outerHTML = $0.outerHTML;在控制臺中,視頻停止播放并消失,只留下盒子陰影。請注意,我在常規網頁上沒有遇到此行為。我在 Chrome 中也沒有遇到這種行為。我想添加我的 UI 元素,但我找不到解決方法。
查看完整描述

1 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

目前尚不清楚您想對視頻本身做什么。但是,我首先會嘗試擺脫 CSS。如果您確實想撕下視頻,然后將其包裝在您自己的 HTML 中并將其放回原來的位置,您可以這樣做:


// Get reference to the video element

const videoElement = document.getElementsByTagName('video')[0];

// Clone the element

const videoClone = videoElement.cloneNode(true);

// Create your new container

const videoContainer = document.createElement('div');

// Do what you want with the new container

const someHeading = document.createElement('h1');

someHeading.innerText = 'This is a video';

// Append stuff to the new container

videoContainer.append(someHeading);

// Append the cloned video to the new container

videoContainer.append(videoClone);

// Remove the old video

videoElement.remove();

// Append your new video container with cloned video

document.body.append(videoContainer);

<video width="320" height="240" controls>

  <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">

  Your browser does not support the video tag.

</video>

設置outerHTML只會覆蓋 HTML。如果您想看到差異,您可以嘗試 和 的設置innerHTML,outerHTML但就您而言,結果可能相同。



查看完整回答
反對 回復 2023-10-10
  • 1 回答
  • 0 關注
  • 100 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號