ckeditor - CKEditor 5 按属性查找模型元素
问题描述
我的编辑器中存在一个自定义 html dom 元素:
<content id="1">
<p>Paragraf is here and <a href="/url/to">link text</a> exists</p>
</content>
我想使用它的属性在模型中找到这个元素。我可以使用从根开始的递归方法找到它。但是有没有像 querySelectorAll 这样更简单的方法?
解决方案
我还没有找到一个开箱即用的帮助方法,但是如果有人只是在寻找他们可以使用的函数定义,试试这个:
function findDescendant (modelElement, predicate) {
if (predicate(modelElement)) {
return modelElement
}
if (modelElement.getChildren) {
for (let child of modelElement.getChildren()) {
const found = findDescendantByName(child, name)
if (found) {
return found
}
}
}
}
...
...
const descendant = findDescendant(element, (element) => element.hasAttribute('foobar'))
推荐阅读
- python - 打印 2D NumPy 数组时如何删除外括号
- flutter - 更改格式日期时间
- javascript - 使用 insertAdjacentHTML 会在 .js 文件中出现错误,但在控制台片段中却没有
- javascript - 如何在导航栏外发起点击以将其关闭
- java - 为什么 if 语句“if(1)”在 Java 中会出错,但在 C++ 中却是正确的?
- javascript - 高亮文本字段的逻辑操作
- php - 用PHP生成中间点的分页
- tensorflow - 最快的 Mask R-CNN 实现是什么
- powershell - 在 Powershell 中从 JSON 中删除项目
- algorithm - 比较或替换操作的时间复杂度是多少?