Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否有一种通用的方法来区分格式化标签(如 B、BR、P 等)和包含标签(如 DIV、SPAN 等)的文本。使用 Javascript。即使格式化标签中包含内容(例如 B 或 P 标签中包含内容),该方法也应该有效。
通过标记名选择元素可以通过使用document.getElementsByTagName或来实现document.querySelectorAll。后者可能适合您的需求,例如
document.getElementsByTagName
document.querySelectorAll
var nontxt = document.querySelectorAll('b, i, em, hr, br');
如果您需要文档中的所有空元素,请尝试使用此 (css3) 选择器
var nontxt = document.querySelectorAll(':empty');