久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    1. <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 class="qwawimqqmiuu" id='cc7Rf'><tfoot id='cc7Rf'></tfoot><dl id='cc7Rf'><fieldset id='cc7Rf'></fieldset></dl></div>
    2. <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 僅設置數組中最后一項的屬性

      Javascript only sets attribute for last item in array(Javascript 僅設置數組中最后一項的屬性)
      <legend id='FBEdj'><style id='FBEdj'><dir id='FBEdj'><q id='FBEdj'></q></dir></style></legend>
          <tbody id='FBEdj'></tbody>
        • <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 class="qwawimqqmiuu" id='FBEdj'><tfoot id='FBEdj'></tfoot><dl id='FBEdj'><fieldset id='FBEdj'></fieldset></dl></div>
              • 本文介紹了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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Use IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
                anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 項目中不起作用)
                Ionic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用異步數據更新 Observable)
                Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
                In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何創建使用 Ionic 組件的自定義指令?)
                Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(將 ViewChild 用于動態元素 - Angular 2 amp;離子2)

                • <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'>

                        <tbody id='FKuq0'></tbody>
                      <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 class="qwawimqqmiuu" id='FKuq0'><tfoot id='FKuq0'></tfoot><dl id='FKuq0'><fieldset id='FKuq0'></fieldset></dl></div>

                        1. 主站蜘蛛池模板: 999在线精品 | 久草中文在线 | 午夜免费在线电影 | 四虎影院免费在线播放 | 欧美激情视频一区二区三区在线播放 | 亚洲国产精品区 | 国产中的精品av涩差av | 日本午夜网站 | 中文字幕视频在线观看 | 久久综合九色综合欧美狠狠 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | 婷婷福利视频导航 | 日日爽 | 国产精品久久久久久吹潮 | 国产乱一区二区三区视频 | 亚洲欧美在线视频 | 中文字幕亚洲区一区二 | 亚洲精品二区 | 国产精品免费在线 | 亚洲毛片在线 | 久久在线 | 国产在线视频一区二区董小宛性色 | 亚洲精品一区中文字幕乱码 | 成人av一区 | 日本黄色片免费在线观看 | 亚洲国产精品一区 | 一级黄色片日本 | 国产精品久久久久久中文字 | 国产精品区二区三区日本 | 欧美在线观看一区 | 一本一道久久a久久精品蜜桃 | 九九亚洲 | 久久久久久黄 | aaaaa毛片 | 99在线观看视频 | 国产精品一区二区在线播放 | 国产99久久 | 99热免费在线 | www.操com | 久草色播| 韩三级在线观看 |