首页 \ 问答 \ 使用forEach方法更改项目的属性(javascript)(Change Properties of Items using a forEach Method (javascript))

使用forEach方法更改项目的属性(javascript)(Change Properties of Items using a forEach Method (javascript))

我正在使用javascript forEach方法迭代数组中的项目,但是当我尝试使用项目执行某些操作时,它会引发错误。

在下面的代码中,当我使用控制台登录'item'时,它会给控制台记录'div1,div2,div3'所需的行为。 但是,当我尝试对这些项目进行更改时,它将不允许这样做。 在Google中使用forEach方法的例子非常抽象。

如何使用此方法更改项目的背景颜色或其他属性?

Codepen链接在这里https://codepen.io/emilychews/pen/boJBKB

JS

var div1 = document.getElementById('div1');
var div2 = document.getElementById('div2');
var div3 = document.getElementById('div3');

var myArray = ['div1','div2','div3']

myArray.forEach(function(item){

  console.log(item);

  item.style.backgroundColor = "blue";  

})

CSS

.div {height: 50px; width: 50px; background-color: red; margin-bottom: 10px;}

HTML

<div class="div" id="div1"></div>
<div class="div" id="div2"></div>
<div class="div" id="div3"></div>

任何帮助都是极好的

艾米莉


I'm using a javascript forEach method to iterate over items in an array, but it's throwing an error when I try and do something with the items.

In the code below when I console log 'item' it gives the desired behavior of logging 'div1, div2, div3' to the console. However, when I try and make a change to these items it won't allow it. The examples of using a forEach method in JS when you Google it is very abstract.

How do I use this method to change the background colors or other properties of the items?

Codepen link is here https://codepen.io/emilychews/pen/boJBKB

JS

var div1 = document.getElementById('div1');
var div2 = document.getElementById('div2');
var div3 = document.getElementById('div3');

var myArray = ['div1','div2','div3']

myArray.forEach(function(item){

  console.log(item);

  item.style.backgroundColor = "blue";  

})

CSS

.div {height: 50px; width: 50px; background-color: red; margin-bottom: 10px;}

HTML

<div class="div" id="div1"></div>
<div class="div" id="div2"></div>
<div class="div" id="div3"></div>

Any help would be awesome

Emily


原文:https://stackoverflow.com/questions/46868297
更新时间:2023-09-05 15:09

最满意答案

options(stringsAsFactors = FALSE)

options(stringsAsFactors = FALSE)

相关问答

更多

相关文章

更多

最新问答

更多
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的
  • SimplePie问题(SimplePie Problem)
  • 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
  • HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
  • 为什么我会收到链接错误?(Why do I get a linker error?)
  • 如何正确定义析构函数(How to properly define destructor)
  • 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
  • 类型不匹配 - JavaScript(Type mismatch - JavaScript)
  • 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
  • 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
  • 如何开启mysql计划事件
  • 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
  • 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
  • java日历格式(java Calendar format)
  • Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
  • 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
  • LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
  • 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
  • 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
  • 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
  • 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
  • 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
  • Assimp详细说明typedef(Assimp elaborated type refers to typedef)
  • 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
  • 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
  • Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)