javascript - 使用javascript在shadow root中获取html元素
问题描述
我有示例HTML代码:
<section class="search-module">
#shadow-root (open)
<div class="app">
<div class="title">Product Title</div>
</div>
</section>
使用此代码,我可以访问shadow root
元素父容器:
var searchModule = document.getElementsByClassName("search-module").item(0);
但无法shadow root
使用以下命令获取容器内的元素:
searchModule.getElementsByClassName("title") // undefined
解决方案
你必须先导航到shadow-root
然后你才能得到它:
const searchModule = document.querySelector('.search-module');
const searchModuleRoot = searchModule && searchModule.shadowRoot;
const title = searchModuleRoot.querySelector('.title');
推荐阅读
- r - 如何在 Shiny 应用程序中通过输入列名来排列数据表?
- json - Dart 如何访问文件映射数据
- python - 使用 Python 计算接收到的 Midi 输入的数量
- node.js - 在 NodeJS 中可靠地从 FIFO 中读取
- excel - 将公式应用于特定单元格
- javascript - JavaScript - 将 JSON 数据从 Eventbrite API 导入谷歌地图的最佳方式
- python - 来自邮递员的 Django POST 请求
- javascript - 如何修复 npm install 的错误?
- javascript - React 中是否有用于原子操作的信号量
- reactjs - 单击按钮后显示输入字段 react-native