为什么我对附加到父div的元素执行console.log时会出现一堆回车节点。有没有办法删除它,因为当我尝试向这些添加一些CSS时,他们打破了应用程序。
这是我的设置。我正在使用下划线模板来创建我的html。 tmpl一切正常。
var tmpl = $( template({items : list}) );
var items = $(_.filter(tmpl, function(item){
return ! $item.hasClass('pin');
}));
var domItems = items.appendTo($('.container'));
这是日志
domItems =
[<TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item, <TextNode textContent="\n \n ">, div.item]
我只是希望它返回div.item元素
答案 0 :(得分:2)
有些浏览器将空格视为文本节点。要过滤它们,只需排除等于nodeType
(Node.TEXT_NODE == 3
)的所有3
:
var items = $(_.filter(tmpl, function(item){
return ! $item.hasClass('pin') && this.nodeType !== 3;
}));