如何查找所有 contenteditable true 的 DOM 节点

M.O*_*huk 1 javascript dom

如果没有 jQuery,如何找到属性contenteditable为 true 的所有 DOM 节点?

我认为必须有更聪明的方法来做到这一点,而不是通过属性检查来获取所有元素div和元素。pdocument.getElementsByTagName

Has*_*mam 6

您可以使用document.querySelectorAll('[contenteditable=true]')

document.querySelectorAll返回文档中与指定选择器组匹配的元素列表(使用文档节点的深度优先前序遍历)。返回的对象是一个 NodeList。

console.log(document.querySelectorAll('[contenteditable=true]'))
Run Code Online (Sandbox Code Playgroud)
<p contenteditable="true">This is a paragraph. It is editable. Try to change this text.</p>
<p contenteditable="false">This is a paragraph. It is editable. Try to change this text.</p>
<div contenteditable="true">This is a paragraph. It is editable. Try to change this text.</div>
Run Code Online (Sandbox Code Playgroud)