pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何判斷一個元素是否在影子 DOM 中?

How can I tell if an element is in a shadow DOM?(如何判斷一個元素是否在影子 DOM 中?)
本文介紹了如何判斷一個元素是否在影子 DOM 中?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個項目,我在本地使用影子 DOM(而不是通過 polyfill).我想檢測給定的 element 是否包含在 shadow DOM 或 light DOM 中.

我查看了元素的所有屬性,但似乎沒有任何根據元素所在的 DOM 類型而有所不同.

如何確定一個元素是陰影 DOM 還是光 DOM 的一部分?

<小時>

以下是出于本問題的目的被認為是shadow DOM"和light DOM"的示例.

<上一頁>(輕根) ? 文檔(淺色) ? HTML(光) |? 身體(光) |? DIV(影根) |? 影子根(影子)|? 分區(影子)|? 框架(輕根) |? 文檔(光) |? HTML(光) ||? 身體(光) ||? 分區(影根) ||? 影子根(影子)||? 分區(無)|? [第二個文檔的未附加 DIV](無) ? [第一個文檔的未附加 DIV]

<!doctype html><標題>isInShadow() 測試文檔 - 無法在 Stack Exchange 的沙箱中運行</標題><iframe src="about:blank"></iframe><腳本>函數isInShadow(元素){//去做}功能測試() {//(輕根) ? 文檔//(淺色) ? HTMLvar html = document.documentElement;console.assert(isInShadow(html) === false);//(光)|? 身體var body = document.body;console.assert(isInShadow(body) === false);//(光)|? DIVvar div = document.createElement('div');body.appendChild(div);console.assert(isInShadow(div) === false);//(影子根) |? 影子根var divShadow = div.createShadowRoot();var shadowDiv = document.createElement('div');divShadow.appendChild(shadowDiv);//(陰影) |? 分區console.assert(isInShadow(shadowDiv) === true);//(陰影) |? 框架var iframe = document.querySelector('iframe');shadowDiv.appendChild(iframe);console.assert(isInShadow(iframe) === true);//(輕根) |? 文檔var iframeDocument = iframe.contentWindow.document;//(光)|? HTMLvar iframeHtml = iframeDocument.documentElement;console.assert(isInShadow(iframeHtml) === false);//(光)||? 身體var iframeBody = iframeDocument.body;//console.assert(isInShadow(iframeHtml) === false);//(光)||? 分區var iframeDiv = iframeDocument.createElement('div');iframeBody.appendChild(iframeDiv);console.assert(isInShadow(iframeDiv) === false);//(影子根) ||? 影子根var iframeDivShadow = iframeDiv.createShadowRoot();//(陰影) ||? 分區var iframeDivShadowDiv = iframeDocument.createElement('div');iframeDivShadow.appendChild(iframeDivShadowDiv);console.assert(isInShadow(iframeDivShadowDiv) === true);//(無) |? [第二個文檔的未附加 DIV]var iframeUnattached = iframeDocument.createElement('div');console.assert(Boolean(isInShadow(iframeUnattached)) === false);//(無) ? [第一個文檔的未附加 DIV]var rootUnattached = document.createElement('div');console.assert(Boolean(isInShadow(rootUnattached)) === false);}加載 = 函數 main() {console.group('測試');嘗試 {測試();console.log('測試完成.');} 最后 {控制臺.groupEnd();}}</script>

解決方案

如果調用ShadowRoot的toString()方法,會返回"[object ShadowRoot]".根據這個事實,這是我的方法:

function isInShadow(node) {var parent = (node && node.parentNode);而(父){if(parent.toString() === "[object ShadowRoot]") {返回真;}父 = 父節點;}返回假;}

<小時>

編輯

Jeremy Banks 提出了另一種循環方式的方法.這種方法和我的有點不同:它還會檢查傳遞的節點本身,而我沒有這樣做.

function isInShadow(node) {for (; node; node = node.parentNode) {if (node.toString() === "[object ShadowRoot]") {返回真;}}返回假;}

function isInShadow(node) {for (; node; node = node.parentNode) {if (node.toString() === "[object ShadowRoot]") {返回真;}}返回假;}console.group('測試');var lightElement = document.querySelector('div');console.assert(isInShadow(lightElement) === false);var shadowChild = document.createElement('div');lightElement.createShadowRoot().appendChild(shadowChild);console.assert(isInShadow(shadowChild) === true);var orphanedElement = document.createElement('div');console.assert(isInShadow(orphanedElement) === false);var orphanedShadowChild = document.createElement('div');orphanedElement.createShadowRoot().appendChild(orphanedShadowChild);console.assert(isInShadow(orphanedShadowChild) === true);var fragmentChild = document.createElement('div');document.createDocumentFragment().appendChild(fragmentChild);console.assert(isInShadow(fragmentChild) === false);console.log('完成.');console.groupEnd();

<div></div>

I have a project where I'm using the shadow DOM natively (not through a polyfill). I'd like to detect if a given element is contained within a shadow DOM or a light DOM.

I've looked through all of the properties on the elements, but there don't seem to be any which vary based on the type of DOM an element is in.

How can I determine if an element is part of a shadow DOM or a light DOM?


Here is an example of what is considered "shadow DOM" and "light DOM" for the purpose of this question.

 (light root) ? Document
      (light)   ? HTML
      (light)   | ? BODY
      (light)   |   ??DIV
(shadow root)   |     ? ShadowRoot
     (shadow)   |       ? DIV 
     (shadow)   |         ? IFRAME 
 (light root)   |           ? Document
      (light)   |             ? HTML
      (light)   |             | ? BODY
      (light)   |             |   ? DIV
(shadow root)   |             |     ? ShadowRoot
     (shadow)   |             |       ? DIV
       (none)   |             ? [Unattached DIV of second Document]
       (none)   ? [Unattached DIV of first Document]

<!doctype html>
<title>
  isInShadow() test document - can not run in Stack Exchange's sandbox
</title>
<iframe src="about:blank"></iframe>
<script>

function isInShadow(element) {
  // TODO
}

function test() {
  //  (light root) ? Document
  //       (light)   ? HTML
  var html = document.documentElement;

  console.assert(isInShadow(html) === false);

  //       (light)   | ? BODY
  var body = document.body;

  console.assert(isInShadow(body) === false);

  //       (light)   |   ??DIV
  var div = document.createElement('div');
  body.appendChild(div);

  console.assert(isInShadow(div) === false);

  // (shadow root)   |     ? ShadowRoot
  var divShadow = div.createShadowRoot();

  var shadowDiv = document.createElement('div');
  divShadow.appendChild(shadowDiv);

  //      (shadow)   |       ? DIV 
  console.assert(isInShadow(shadowDiv) === true);

  //      (shadow)   |         ? IFRAME 
  var iframe = document.querySelector('iframe');
  shadowDiv.appendChild(iframe);

  console.assert(isInShadow(iframe) === true);

  //  (light root)   |           ? Document
  var iframeDocument = iframe.contentWindow.document;

  //       (light)   |             ? HTML
  var iframeHtml = iframeDocument.documentElement;

  console.assert(isInShadow(iframeHtml) === false);

  //       (light)   |             | ? BODY
  var iframeBody = iframeDocument.body;

  //
  console.assert(isInShadow(iframeHtml) === false);

  //       (light)   |             |   ? DIV
  var iframeDiv = iframeDocument.createElement('div');
  iframeBody.appendChild(iframeDiv);
   
  console.assert(isInShadow(iframeDiv) === false);
   
  // (shadow root)   |             |     ? ShadowRoot
  var iframeDivShadow = iframeDiv.createShadowRoot();

  //      (shadow)   |             |       ? DIV
  var iframeDivShadowDiv = iframeDocument.createElement('div');
  iframeDivShadow.appendChild(iframeDivShadowDiv);
    
  console.assert(isInShadow(iframeDivShadowDiv) === true);
     
  //        (none)   |             ? [Unattached DIV of second Document]
  var iframeUnattached = iframeDocument.createElement('div');
    
  console.assert(Boolean(isInShadow(iframeUnattached)) === false);

  //        (none)   ? [Unattached DIV of first Document]
  var rootUnattached = document.createElement('div');
    
  console.assert(Boolean(isInShadow(rootUnattached)) === false);
}

onload = function main() {
  console.group('Testing');
  try {
    test();
    console.log('Testing complete.');
  } finally {
    console.groupEnd();
  }
}

</script>

解決方案

If you call a ShadowRoot's toString() method, it will return "[object ShadowRoot]". According to this fact, here's my approach:

function isInShadow(node) {
    var parent = (node && node.parentNode);
    while(parent) {
        if(parent.toString() === "[object ShadowRoot]") {
            return true;
        }
        parent = parent.parentNode;
    }
    return false;
}


EDIT

Jeremy Banks suggests an approach in another style of looping. This approach is a little different from mine: it also checks the passed node itself, which I didn't do.

function isInShadow(node) {
    for (; node; node = node.parentNode) {
        if (node.toString() === "[object ShadowRoot]") {
            return true;
        }
    }
    return false;
}

function isInShadow(node) {
    for (; node; node = node.parentNode) {
        if (node.toString() === "[object ShadowRoot]") {
            return true;
        }
    }
    return false;
}

console.group('Testing');

var lightElement = document.querySelector('div');    

console.assert(isInShadow(lightElement) === false);

var shadowChild = document.createElement('div');
lightElement.createShadowRoot().appendChild(shadowChild);

console.assert(isInShadow(shadowChild) === true);

var orphanedElement = document.createElement('div');

console.assert(isInShadow(orphanedElement) === false);

var orphanedShadowChild = document.createElement('div');
orphanedElement.createShadowRoot().appendChild(orphanedShadowChild);

console.assert(isInShadow(orphanedShadowChild) === true);

var fragmentChild = document.createElement('div');
document.createDocumentFragment().appendChild(fragmentChild);

console.assert(isInShadow(fragmentChild) === false);

console.log('Complete.');
console.groupEnd();

<div></div>

這篇關于如何判斷一個元素是否在影子 DOM 中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Is Math.random() cryptographically secure?(Math.random() 在密碼學上是安全的嗎?)
Secure random numbers in javascript?(在javascript中保護隨機數?)
How to avoid multiple token refresh requests when making simultaneous API requests with an expired token(使用過期令牌發出同時 API 請求時如何避免多個令牌刷新請求)
JWT not decoding quot;JWT malformedquot; - Node Angular(JWT 未解碼“JWT malformed;- 節點角度)
How to invalidate a JWT token with no expiry time(如何使沒有到期時間的 JWT 令牌無效)
Authorization header in img src link(img src 鏈接中的授權標頭)
主站蜘蛛池模板: 行业分析:提及郑州火车站附近真有 特殊按摩 ?2025实地踩坑指南 新手如何避坑不踩雷 | 圣才学习网-考研考证学习平台,提供万种考研考证电子书、题库、视频课程等考试资料 | 警方提醒:赣州约炮论坛真的安全吗?2025年新手必看的网络交友防坑指南 | 电缆桥架生产厂家_槽式/梯式_热镀锌线槽_广东东莞雷正电气 | 食品无尘净化车间,食品罐装净化车间,净化车间配套风淋室-青岛旭恒洁净技术有限公司 | 精密光学实验平台-红外粉末压片机模具-天津博君 | 山东彩钢板房,山东彩钢活动房,临沂彩钢房-临沂市贵通钢结构工程有限公司 | 聚氨酯保温钢管_聚氨酯直埋保温管道_聚氨酯发泡保温管厂家-沧州万荣防腐保温管道有限公司 | 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 365文案网_全网创意文案句子素材站 | 翻斗式矿车|固定式矿车|曲轨侧卸式矿车|梭式矿车|矿车配件-山东卓力矿车生产厂家 | 亿诺千企网-企业核心产品贸易 | 液氮罐_液氮容器_自增压液氮罐_杜瓦瓶_班德液氮罐厂家 | 送料机_高速冲床送料机_NC伺服滚轮送料机厂家-东莞市久谐自动化设备有限公司 | 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 纸布|钩编布|钩针布|纸草布-莱州佳源工艺纸布厂 | 河南正规膏药生产厂家-膏药贴牌-膏药代加工-修康药业集团官网 | 微波萃取合成仪-电热消解器价格-北京安合美诚科学仪器有限公司 | 压力控制器,差压控制器,温度控制器,防爆压力控制器,防爆温度控制器,防爆差压控制器-常州天利智能控制股份有限公司 | 污泥烘干机-低温干化机-工业污泥烘干设备厂家-焦作市真节能环保设备科技有限公司 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 河南彩印编织袋,郑州饲料编织袋定制,肥料编织袋加工厂-盛军塑业 河南凯邦机械制造有限公司 | 广东机电安装工程_中央空调工程_东莞装饰装修-广东粤标建设有限公司 | 大数据营销公司_舆情监测软件_上海SEO公司-文军营销官网 | 液压油缸-液压站生产厂家-洛阳泰诺液压科技有限公司 | 旗帜网络笔记-免费领取《旗帜网络笔记》电子书 | 米顿罗计量泵(科普)——韬铭机械| PAS糖原染色-CBA流式多因子-明胶酶谱MMP-上海研谨生物科技有限公司 | 地埋式垃圾站厂家【佳星环保】小区压缩垃圾中转站转运站 | 上海风淋室_上海风淋室厂家_上海风淋室价格_上海伯淋 | 三板富 | 专注于新三板的第一垂直服务平台 | 低噪声电流前置放大器-SR570电流前置放大器-深圳市嘉士达精密仪器有限公司 | 丹佛斯变频器-Danfoss战略代理经销商-上海津信变频器有限公司 | 超细|超微气流粉碎机|气流磨|气流分级机|粉体改性机|磨粉机|粉碎设备-山东埃尔派粉体科技 | 事迹材料_个人事迹名人励志故事| 中国品牌排名投票_十大品牌榜单_中国著名品牌【中国品牌榜】 | 齿轮减速机电机一体机_齿轮减速箱加电机一体化-德国BOSERL蜗轮蜗杆减速机电机生产厂家 | 光照全温振荡器(智能型)-恒隆仪器 | 称重传感器,测力传感器,拉压力传感器,压力变送器,扭矩传感器,南京凯基特电气有限公司 |