<legend id='FBEdj'><style id='FBEdj'><dir id='FBEdj'><q id='FBEdj'></q></dir></style></legend>
    1. <small id='FBEdj'></small><noframes id='FBEdj'>

      <tfoot id='FBEdj'></tfoot>

          <bdo id='FBEdj'></bdo><ul id='FBEdj'></ul>
        <i id='FBEdj'><tr id='FBEdj'><dt id='FBEdj'><q id='FBEdj'><span id='FBEdj'><b id='FBEdj'><form id='FBEdj'><ins id='FBEdj'></ins><ul id='FBEdj'></ul><sub id='FBEdj'></sub></form><legend id='FBEdj'></legend><bdo id='FBEdj'><pre id='FBEdj'><center id='FBEdj'></center></pre></bdo></b><th id='FBEdj'></th></span></q></dt></tr></i><div id='FBEdj'><tfoot id='FBEdj'></tfoot><dl id='FBEdj'><fieldset id='FBEdj'></fieldset></dl></div>
      1. Javascript 仅设置数组中最后一项的属性

        时间:2023-09-08
          <tbody id='cc7Rf'></tbody>

              • <i id='cc7Rf'><tr id='cc7Rf'><dt id='cc7Rf'><q id='cc7Rf'><span id='cc7Rf'><b id='cc7Rf'><form id='cc7Rf'><ins id='cc7Rf'></ins><ul id='cc7Rf'></ul><sub id='cc7Rf'></sub></form><legend id='cc7Rf'></legend><bdo id='cc7Rf'><pre id='cc7Rf'><center id='cc7Rf'></center></pre></bdo></b><th id='cc7Rf'></th></span></q></dt></tr></i><div id='cc7Rf'><tfoot id='cc7Rf'></tfoot><dl id='cc7Rf'><fieldset id='cc7Rf'></fieldset></dl></div>
              • <tfoot id='cc7Rf'></tfoot>
                <legend id='cc7Rf'><style id='cc7Rf'><dir id='cc7Rf'><q id='cc7Rf'></q></dir></style></legend>

                <small id='cc7Rf'></small><noframes id='cc7Rf'>

                • <bdo id='cc7Rf'></bdo><ul id='cc7Rf'></ul>
                  本文介绍了Javascript 仅设置数组中最后一项的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个约 50 个项目的数组,在 javascript 中,它通过这个数组来检查一个条件,如果满足条件,它会为要检查的复选框设置属性.它通过得很好,但每次只更改数组中的最后一项.这是一段代码.

                  I have an array of ~50 items and in javascript it goes through this array to check a condition and if the condition is met it setsattribute for checkboxes on the be checked. It goes through nicely but only changes the very final item in the array every time. Here is a section of the code.

                  for (i = 0; i < urlArray.length; i++) {
                      var newImg = new Image();
                      var coName = companyArray[i];
                      newImg.onload = function(){
                          if (condition is met){
                              nStar++;
                  
                                      document.getElementById(coName).setAttribute("checked", "checked");
                                      document.getElementById(coName).checked = true;
                          } else {
                              nStar++;
                  
                                      document.getElementById(coName).setAttribute("checked", "checked");
                                      document.getElementById(coName).checked = true;
                                      document.getElementById(coName).setAttribute("disabled", "disabled");
                                      document.getElementById(coName).disabled = true;        
                          }
                  
                      };
                  

                  所以你可以看到条件是否满足,它仍然会改变属性,但我收到的错误是它只改变了数组中的最后一项.有什么想法吗?

                  So as you can see if the condition is met or not it will still change the attributes but the error I have been receiving is that it only changes the final item in the array. Any ideas?

                  推荐答案

                  这是 Javascript 中异步回调的经典问题.onload 处理程序将在一段时间后调用,在 for 循环完成很久之后,因此 for 循环中的索引被固定在它结束的末尾,并且局部变量 newImg 和 coName 将只有它们在循环中的最后一个值.

                  This is a classic issue with an asynchronous callback in Javascript. The onload handler will be called some time later, long after the for loop has finished, thus the index in the for loop is pegged at the end where it ended up and the local variables newImg and coName will only have their last values in the loop.

                  您希望在 onload 处理程序中使用的任何变量都必须传递到实际的函数闭包中,以便它们对每个不同的 onload 处理程序都是唯一可用的.有几种方法可以做到这一点.

                  Any variables that you wish to use inside the onload handler will have to passed into the actual function closure so that they are uniquely available for each different onload handler. There are several ways to do that.

                  这种方式使用函数闭包来捕获传入的值并使其可用于 onload 函数处理程序:

                  This way uses a function closure to capture the passed in value and make it available to the onload function handler:

                  for (i = 0; i < urlArray.length; i++) {
                      var newImg = new Image();
                      var coName = companyArray[i];
                      newImg.onload = (function (coName) {
                          return function(){
                              if (condition is met){
                                  nStar++;
                                  document.getElementById(coName).setAttribute("checked", "checked");
                                  document.getElementById(coName).checked = true;
                              } else {
                                  nStar++;
                                  document.getElementById(coName).setAttribute("checked", "checked");
                                  document.getElementById(coName).checked = true;
                                  document.getElementById(coName).setAttribute("disabled", "disabled");
                                  document.getElementById(coName).disabled = true;        
                              }
                          };
                      }) (coName);
                  }
                  

                  在 Javascript 语言中,此方法的作用是将执行匿名函数调用的返回值分配给 onload 属性.该匿名函数调用将 coName 参数传递给它.该函数返回另一个匿名函数,该函数被分配为 onload 处理程序.但是,由于函数闭包在 javascript 中的工作方式,coName 的值被捕获在函数闭包中,并且在函数闭包期间保持可供 onload 处理程序访问.可以把它想象成一个函数的实例,它周围有状态(局部变量的值),每次设置时都会被唯一地捕获.在这种情况下,它会捕获 coName 变量的值并将其放入闭包中,在该闭包中它变得唯一,并且不会受到后续对 for 循环中外部 coName 变量的更改的影响.

                  In Javascript-speak, what this method does is assign to the onload attribute the return value from executing an anonymous function call. That anonymous function call passed the coName parameter into it. That function returns another anonymous function which is what gets assigned as the onload handler. But, because of the way function closures work in javascript, the value of coName is captured in the function closure and is kept accessible to the onload handler for the duration of the function closure. One can think of it a little like an instance of a function with state around it (values of local variables) that is uniquely capture each time it's set up. In this case, it captures the value of the coName variable and puts it into the closure where it becomes unique and won't be effected by later changes to the outer coName variable that's in the for loop.

                  另一种方法是将参数放在实际对象上,以便您可以从那里检索它:

                  Another way to do it is to put the parameter on the actual object so you can retrieve it from there:

                  for (i = 0; i < urlArray.length; i++) {
                      var newImg = new Image();
                      newImg.setAttribute("coName". companyArray[i]);
                      newImg.onload = function() {
                          var coName = this.getAttribute("coName");
                          if (condition is met){
                              nStar++;
                              document.getElementById(coName).setAttribute("checked", "checked");
                              document.getElementById(coName).checked = true;
                          } else {
                              nStar++;
                              document.getElementById(coName).setAttribute("checked", "checked");
                              document.getElementById(coName).checked = true;
                              document.getElementById(coName).setAttribute("disabled", "disabled");
                              document.getElementById(coName).disabled = true;        
                          }
                  
                      };
                  }
                  

                  这篇关于Javascript 仅设置数组中最后一项的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:jQuery删除属性 下一篇:选择元素是否具有必需的属性?

                  相关文章

                  最新文章

                  • <bdo id='FKuq0'></bdo><ul id='FKuq0'></ul>
                  <legend id='FKuq0'><style id='FKuq0'><dir id='FKuq0'><q id='FKuq0'></q></dir></style></legend>
                  <tfoot id='FKuq0'></tfoot>

                    <small id='FKuq0'></small><noframes id='FKuq0'>

                    <i id='FKuq0'><tr id='FKuq0'><dt id='FKuq0'><q id='FKuq0'><span id='FKuq0'><b id='FKuq0'><form id='FKuq0'><ins id='FKuq0'></ins><ul id='FKuq0'></ul><sub id='FKuq0'></sub></form><legend id='FKuq0'></legend><bdo id='FKuq0'><pre id='FKuq0'><center id='FKuq0'></center></pre></bdo></b><th id='FKuq0'></th></span></q></dt></tr></i><div id='FKuq0'><tfoot id='FKuq0'></tfoot><dl id='FKuq0'><fieldset id='FKuq0'></fieldset></dl></div>