使用 javascript 从特定 div 标签中删除所有文本节点
我有一些从 php 脚本动态生成的 html。 php代码看起来像这样
echo "<div id='categories'>\n";
foreach($category as $k => $v)
{
echo "<div class='category'>\n";
echo "<div id=\"$k\" class='cat_name'\n>$k</div>\n";
echo "<div id=\"$k".'_link"'." class='cat_link'>\n<a href=$v>Link</a>\n</div>\n";
echo "</div>\n";
}
echo "</div>\n";
我有一个javascript文件尝试像这样访问dom
var categories=document.getElementById('categories').childNodes;
for(i=0;i<categories.length;i++)
{
var link=categories[i].childNodes[1].childNodes[0].attributes['href'];
..
...
..
现在这不能按预期工作,因为html中有文本节点。 当我使用 firebug 控制台
尝试此操作时,
document.getElementById('categories').childNodes[0].nodeType; // displays 3
它显示 3
这意味着该节点是一个文本节点
。 我尝试像这样在 window.onload
中添加 document.normalize();
window.onload=function() {
document.normalize();
var categories=document.getElementById('categories').childNodes;
...
...
但文本节点仍保留在 html 中。我怎样才能删除所有文本节点。现在我认为没有像 getElementByType() 这样的方法,那么我如何获取所有文本节点。
i have some dynamically generated html from a php script. the php code looks like this
echo "<div id='categories'>\n";
foreach($category as $k => $v)
{
echo "<div class='category'>\n";
echo "<div id=\"$k\" class='cat_name'\n>$k</div>\n";
echo "<div id=\"$k".'_link"'." class='cat_link'>\n<a href=$v>Link</a>\n</div>\n";
echo "</div>\n";
}
echo "</div>\n";
i have a javascript file that tries to access the dom like this
var categories=document.getElementById('categories').childNodes;
for(i=0;i<categories.length;i++)
{
var link=categories[i].childNodes[1].childNodes[0].attributes['href'];
..
...
..
now this doesnt work as expected because there are text nodes in the html.
when i used the firebug console
to try this
document.getElementById('categories').childNodes[0].nodeType; // displays 3
it displays 3
which means that the node is a text node
.
i tried adding document.normalize();
in window.onload
like this
window.onload=function() {
document.normalize();
var categories=document.getElementById('categories').childNodes;
...
...
but the text nodes remain in the html. how can i remove all the text nodes. now i dont think there is any method like getElementByType()
so how do i get all text nodes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
.children
而不是.childNodes
仅定位元素(无文本节点):FF3 不支持该属性,因此如果您支持该浏览器,则您'我们需要一种方法来复制它。
另请注意,IE 也会为您提供注释节点,因此如果您的标记不包含注释会更好。
Use
.children
instead of.childNodes
to target elements only (no text nodes):FF3 doesn't support that property, so if you're supporting that browser, you'll need a method to replicate it.
Also note that IE will give you comment nodes as well, so it would be better if your markup didn't include comments.
EE2015语法:
细分:
这只会删除直接子文本节点,而不会删除树中更深层的内容。
with NodeIterator:
EE2015 syntax:
Breakdown:
This will only remove direct-child text nodes and nothing deeper in the tree.
with NodeIterator:
为什么不使用
getElementsByClassName
MDC 文档对于本身不支持它的浏览器,请查看以下实现:http://ejohn.org/blog /getelementsbyclassname-speed-comparison/
Why don't you use
getElementsByClassName
MDC docs instead ?For browsers that do not natively support it look at these implementations : http://ejohn.org/blog/getelementsbyclassname-speed-comparison/
检查循环中的每个节点,如果它是文本节点,则按照您所说的删除它或不执行任何操作。如果它不是文本节点,请对其执行您想要执行的操作。
Check each node in your loop and if it's a text node, then either remove it as you said or do nothing. If it's not a text node, do what action you want to do to it.