简体   繁体   English

如何检查元素是否可以托管 Shadow DOM?

[英]How can i check whether an element can host Shadow DOM?

Elements that allow Shadow DOM are specified here: https://docs.w3cub.com/dom/element/attachshadow/此处指定允许 Shadow DOM 的元素: https : //docs.w3cub.com/dom/element/attachshadow/

How can i validate if an element supports Shadow DOM or not ?如何验证元素是否支持 Shadow DOM?

if (myElement.attachShadow) {
    ...
}

does not seem to work.似乎不起作用。

You could try and catch if unsupported.如果不受支持,您可以尝试捕获。

try {
   let shadowRoot = elem.attachShadow( { mode: "open" } );
   shadowRoot.innerHTML = ...;
   return true;
} catch( err ) {
   return false;
}

You could also turn this into a helper, but if you need to use it several times, it might then be a good idea to store the results after checking each element.你也可以把它变成一个帮助器,但是如果你需要多次使用它,那么在检查每个元素之后存储结果可能是一个好主意。

 function canAttachShadow( elem ) { try { elem.cloneNode().attachShadow( { mode: "open" } ); return true; } catch( err ) { return false; } } console.log( "a", canAttachShadow( document.createElement( "a" ) ) ); console.log( "article", canAttachShadow( document.createElement( "article" ) ) );

If you really wish, you could also use the specs algorithm, which is a combination of valid-custom-element-name and the white list in your article (ie today, "article", "aside", "blockquote", "body", "div", "footer", "h1", "h2", "h3", "h4", "h5", "h6", "header", "main", "nav", "p", "section", or "span" ), however this white list may change in the future, so any code using it would need to get updated along with the specs.如果你真的愿意,你也可以使用规范算法,它是有效自定义元素名称和文章中白名单的组合(即今天, “文章”、“旁白”、“块引用”、“正文” ", "div", "footer", "h1", "h2", "h3", "h4", "h5", "h6", "header", "main", "nav", "p", "section" 或 "span" ),但是此白名单将来可能会更改,因此任何使用它的代码都需要随规范一起更新。

You can try do it like this:你可以尝试这样做:

if (document.myElement.createShadowRoot || document.myElement.attachShadow) {
    // support shadow DOM
} else {
    // not support
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何判断一个元素是否在影子 DOM 中? - How can I tell if an element is in a shadow DOM? 如何在野生动物园的Shadow DOM中找到元素? (聚合物) - How can I find element in shadow DOM in safari? (Polymer) 如何创建带有阴影dom的聚合物自定义元素,以便可以访问它的shadowRoot? - How can I create polymer custom element with shadow dom so that I can access it's shadowRoot? 如何根据类名检查元素是否存在? - How can I check whether element exists based on the classname? 如果DOM元素包含类,我如何检入JavaScript? - How can I check in JavaScript if a DOM element contains a class? 如何检查可见 DOM 中是否存在元素? - How can I check if an element exists in the visible DOM? 如何检查元素是否仍然存在于 DOM 中? - How can I check if an element still exists in the DOM? 如何检查我的DOM是否已包含ID的元素? - How can i check if my DOM already contains an element by id? 如何基于addEventListener上的点击事件通过父元素在shadow dom中select元素? - How can I select elements in shadow dom through the parent element based on the click event on addEventListener? 仅当它是最后一个子元素时,我如何 select 阴影 DOM 宿主元素? - How do I select the shadow DOM host element only if it is the last-child?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM