使用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
最满意答案
options(stringsAsFactors = FALSE)
options(stringsAsFactors = FALSE)
相关问答
更多-
原因是,由于每次在函数调用中使用上面创建的相同对象,您将获得相同的hashkey值,然后将相同的实例添加到数组中。 去掉, var temp ={ "id":0, "LibraryName":"" , "Projects":0, "Status":"", "TaxYear":0 }; 并将其放在createGCA()中。 你的代码看起来像, $scope.createGCA = function ( ...
-
您可以使用同一个类来使用方法完成您所做的一切。 对于那些输入变量,将它们声明为public static,并使用类构造函数来设置它们。 public static $userId; self::$userId = public static $userId; self::$userId = ....和cal GetMesage::userId 。 使用strpos('help', $yourString) ,这里不需要常量。 我不认为你需要在类构造函数中这样做,如果你这样做,你可以添加更多的方法。 就像你在 ...
-
我们假设您有以下内容 >> vec=c(1,1,1,1,0,0,0) >> vecf=as.factor(vec) 然后,以下内容将为您提供所需的结果 >> summary(as.numeric(as.character(vecf))) Let's assume you have the following >> vec=c(1,1,1,1,0,0,0) >> vecf=as.factor(vec) Then the following will give you the desi ...
-
在你的fullname_chk函数中,你可以像往常一样访问任何post / get变量。 在这种情况下,你可以重用$input_data_array = (array)json_decode(file_get_contents('php://input')); 但是除非您想在表单验证库之外进行某种验证,否则没有其他方法可以将额外数据传递给回调。 In your fullname_chk function you can access any post/get variables as you normall ...
-
应用于json输入的融化函数用因子替换一些值(melt function applied to json input replaces some values with factors)[2022-03-22]
options(stringsAsFactors = FALSE) options(stringsAsFactors = FALSE) -
这是因为parse_date()在到达函数末尾时返回None,并且您将元素设置为此值。 相反,返回值不变: def parse_date(datestamp): try: return datetime.datetime.strptime(str(datestamp)[:10], '%Y-%m-%d') except ValueError: return datestamp It's because parse_date() is retur ...
-
jQuery没有得到应用(jQuery is not getting applied)[2022-09-18]
这是因为您的acceptingRequest函数仅在匿名jQuery(document).ready回调中可见 。 因此,当您单击按钮时,acceptingRequest不可见。 保持jQuery(document).ready(function(){})的解决方案 要解决此问题,请使用$('button.acceptinvite').on('click',acceptingRequest)绑定回调内的处理程序 或使用匿名回调(类似这样): $('button.acceptinvite').on('clic ... -
我不相信有一种方法可以直接在melt的调用中做到这一点,但事实上你可以试试这个: df = melt(Canal, id=Canal$value) df$value = as.numeric(as.character(df$value)) 将因子转换为数字将采用因子级别,而您想要的是将因子的标签转换为数值。 因此,以下也应该有效: df$value = as.numeric(labels(df$value)) I don't believe there is a way to do this direct ...
-
看来问题是你每次都没有通过for循环创建一个新的SearchModel实例,所以values.add (data); 只是添加对同一个SearchModel实例的另一个引用。 也就是说,ArrayList中的每个项都指向一个对象。 尝试更改代码,如下所示: protected void onPostExecute(String s) { values = new ArrayList
(); // data = new SearchModel (); << Rem ... -
它反对你的一个变量(如gvrocha所说); 你可能只有一个级别或一个字符串。 快速跟踪有问题的变量的提示是进行间隔对分并增加/减少col索引,直到触发错误为止。 最好使用数字(列索引)接口到glm( glm(data[,'install.'] ~ data[,2:40]) ,见[1]) 而不是公式接口glm(install. ~ var1 + var2 + ... : [1] R中的动态公式创建? It objects to one of your variables (as gvrocha said); ...