javascript - 使用文档选择器选择自定义元素
问题描述
我正在用 Stenciljs 编写一个应用程序,我的自定义元素如下:
<custom-alert alertType="warning" alertId="warningMessage" hide>Be warned</custom-alert>
现在,问题在于通过document.querySelector()
或任何其他方式选择此元素以删除或添加hide
属性。这可以很容易地为标准 HTML 元素完成:
document.querySelector('input').removeAttribute('hide');
如何为我的自定义元素执行此操作?
解决方案
通过添加 id 属性,我能够获得所需的结果
<custom-alert id="myCustomAlert" alertType="warning" alertId="warningMessage" >Be warned</custom-alert>
然后可以隐藏该组件并显示如下:
document.getElementById('myCustomAlert').setAttribute('hidden', 'true');
并显示如下:
document.getElementById('myCustomAlert').removeAttribute('hidden');
推荐阅读
- node.js - 如何将 2checkout 与 reactjs 和 nodejs 集成?
- python - 如何在python(netmiko)中从循环中获取多个输出到外部
- android - Flutter/我的应用程序无法加载到模拟器或真实设备?
- spf - 是否可以合并这 2 条 SPF 记录?
- php - 条纹 PHP 付款
- react-native - 在反应原生的多个导航之间共享redux存储
- c# - 如何使用 ASP.net Core 使用小于 5 的 ITextsharp 版本将 HTML 模板转换为 PDF?
- mysql - mysqlinstanceconfig 和禁用严格模式
- c++ - 手动填充 std::match_results
- javascript - Discord.js Ping 命令