Javascript coffeescript-数组筛选不工作
我试图得到一个数组,其中只包含具有background image属性的元素,但是,我的代码不起作用,我不确定出了什么问题Javascript coffeescript-数组筛选不工作,javascript,arrays,coffeescript,filtering,Javascript,Arrays,Coffeescript,Filtering,我试图得到一个数组,其中只包含具有background image属性的元素,但是,我的代码不起作用,我不确定出了什么问题 elements = document.getElementsByTagName("*") [].filter.call elements, (el) => if el.currentStyle return el.currentStyle['backgroundImage'] isnt 'none' else if window.getCompute
elements = document.getElementsByTagName("*")
[].filter.call elements, (el) =>
if el.currentStyle
return el.currentStyle['backgroundImage'] isnt 'none'
else if window.getComputedStyle
return document.defaultView.getComputedStyle(el,null).getPropertyValue('background-image') isnt 'none'
据报道,
filter()
方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素
因此,在使用过滤后的数组之前,需要先存储它:
elements = document.getElementsByTagName("*")
filteredElements = [].filter.call elements, (el) =>
if el.currentStyle
return el.currentStyle['backgroundImage'] isnt 'none'
else if window.getComputedStyle
return document.defaultView.getComputedStyle(el,null).getPropertyValue('background-image') isnt 'none'
// filteredElements is your new array with the filtered elements.
“它怎么不起作用”?在哪一页上应用它?预期的结果是什么,会发生什么?如果我在筛选器调用之前和之后注销元素,结果是相同的。哦,你期望的是什么,你不会在任何地方返回筛选后的数组,请尝试以下操作->@EvanWard:
筛选器
不会更改元素
。它确实返回一个新数组。