首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

jQuery 為 embed 標籤動態新增 allowFullScreen 屬性

     編輯:程式設計疑難解答

問題

HTML 中的 <embed> 標籤缺少 allowFullScreen 屬性,需透過 jQuery 動態添加。

正確代碼

// 方法一:使用屬性選擇器(embed 無 id 時)
$("embed").attr("allowFullScreen", "true");

// 方法二:若有多個 embed,針對特定 src 或 index
$("embed[src*='player']").attr("allowFullScreen", "true");
// 或
$("embed").eq(0).attr("allowFullScreen", "true");

注意事項

  • attr() 設定的是 **HTML 屬性**,對應 DOM 的 attributes 集合。
  • allowFullScreen 為布林屬性,HTML5 標準寫法為 allowfullscreen(全小寫),現代瀏覽器兩者皆支援,建議統一小寫:.attr("allowfullscreen", "") 或 .prop("allowFullScreen", true)。
  • <embed> 為替代元素,部分舊版瀏覽器需在父物件 <object> 上同步設定。

完整範例

<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <embed src="movie.swf" width="480" height="400" />
    <script>
        $(function() {
            $("embed").prop("allowFullScreen", true);  // 推薦用 prop 設定布林屬性
            console.log($("embed").prop("allowFullScreen")); // true
        });
    </script>
</body>
</html>

總結

使用 $("embed").prop("allowFullScreen", true) 或 .attr("allowfullscreen", "") 即可為 embed 標籤啟用全螢幕權限。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved