pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    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="kyc2s2s" 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 僅設(shè)置數(shù)組中最后一項的屬性

      Javascript only sets attribute for last item in array(Javascript 僅設(shè)置數(shù)組中最后一項的屬性)
      <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="m2gq2ik" id='FBEdj'><tfoot id='FBEdj'></tfoot><dl id='FBEdj'><fieldset id='FBEdj'></fieldset></dl></div>
              • 本文介紹了Javascript 僅設(shè)置數(shù)組中最后一項的屬性的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我有一個約 50 個項目的數(shù)組,在 javascript 中,它通過這個數(shù)組來檢查一個條件,如果滿足條件,它會為要檢查的復(fù)選框設(shè)置屬性.它通過得很好,但每次只更改數(shù)組中的最后一項.這是一段代碼.

                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;        
                        }
                
                    };
                

                所以你可以看到條件是否滿足,它仍然會改變屬性,但我收到的錯誤是它只改變了數(shù)組中的最后一項.有什么想法嗎?

                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 中異步回調(diào)的經(jīng)典問題.onload 處理程序?qū)⒃谝欢螘r間后調(diào)用,在 for 循環(huán)完成很久之后,因此 for 循環(huán)中的索引被固定在它結(jié)束的末尾,并且局部變量 newImg 和 coName 將只有它們在循環(huán)中的最后一個值.

                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 處理程序中使用的任何變量都必須傳遞到實際的函數(shù)閉包中,以便它們對每個不同的 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.

                這種方式使用函數(shù)閉包來捕獲傳入的值并使其可用于 onload 函數(shù)處理程序:

                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 語言中,此方法的作用是將執(zhí)行匿名函數(shù)調(diào)用的返回值分配給 onload 屬性.該匿名函數(shù)調(diào)用將 coName 參數(shù)傳遞給它.該函數(shù)返回另一個匿名函數(shù),該函數(shù)被分配為 onload 處理程序.但是,由于函數(shù)閉包在 javascript 中的工作方式,coName 的值被捕獲在函數(shù)閉包中,并且在函數(shù)閉包期間保持可供 onload 處理程序訪問.可以把它想象成一個函數(shù)的實例,它周圍有狀態(tài)(局部變量的值),每次設(shè)置時都會被唯一地捕獲.在這種情況下,它會捕獲 coName 變量的值并將其放入閉包中,在該閉包中它變得唯一,并且不會受到后續(xù)對 for 循環(huán)中外部 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.

                另一種方法是將參數(shù)放在實際對象上,以便您可以從那里檢索它:

                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;        
                        }
                
                    };
                }
                

                這篇關(guān)于Javascript 僅設(shè)置數(shù)組中最后一項的屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

                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 - 使用異步數(shù)據(jù)更新 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 中,如何創(chuàng)建使用 Ionic 組件的自定義指令?)
                Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(將 ViewChild 用于動態(tài)元素 - 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="s0aeoom" id='FKuq0'><tfoot id='FKuq0'></tfoot><dl id='FKuq0'><fieldset id='FKuq0'></fieldset></dl></div>

                        1. 主站蜘蛛池模板: 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 袋式过滤器,自清洗过滤器,保安过滤器,篮式过滤器,气体过滤器,全自动过滤器,反冲洗过滤器,管道过滤器,无锡驰业环保科技有限公司 | 污水提升器,污水提升泵,污水提升装置-德国泽德(zehnder)水泵系统有限公司 | 步入式高低温测试箱|海向仪器 | 海尔生物医疗四川代理商,海尔低温冰箱四川销售-成都壹科医疗器械有限公司 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | 购买舔盐、舔砖、矿物质盐压块机,鱼饵、鱼饲料压块机--请到杜甫机械 | 算命免费_生辰八字_免费在线算命 - 卜算子算命网 | 安全,主动,被动,柔性,山体滑坡,sns,钢丝绳,边坡,防护网,护栏网,围栏,栏杆,栅栏,厂家 - 护栏网防护网生产厂家 | 洁净实验室工程-成都手术室净化-无尘车间装修-四川华锐净化公司-洁净室专业厂家 | 政府园区专业委托招商平台_助力企业选址项目快速落地_东方龙商务集团 | 办公室家具_板式办公家具定制厂家-FMARTS福玛仕办公家具 | 伶俐嫂培训学校_月嫂培训班在哪里报名学费是多少_月嫂免费政府培训中心推荐 | 香港新时代国际美容美发化妆美甲培训学校-26年培训经验,值得信赖! | 北京三友信电子科技有限公司-ETC高速自动栏杆机|ETC机柜|激光车辆轮廓测量仪|嵌入式车道控制器 | 联系我们-腾龙公司上分客服微信19116098882| 挤出机_橡胶挤出机_塑料挤出机_胶片冷却机-河北伟源橡塑设备有限公司 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 智能风向风速仪,风速告警仪,数字温湿仪,综合气象仪(气象五要素)-上海风云气象仪器有限公司 | 广东成考网-广东成人高考网 | 长沙发电机-湖南发电机-柴油发电机供应厂家-长沙明邦智能科技 | 酒精检测棒,数显温湿度计,酒安酒精测试仪,酒精检测仪,呼气式酒精检测仪-郑州欧诺仪器有限公司 | 小程序开发公司-小程序制作-微信小程序开发-小程序定制-咏熠软件 | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 超声波破碎仪-均质乳化机(供应杭州,上海,北京,广州,深圳,成都等地)-上海沪析实业有限公司 | 酒瓶_酒杯_玻璃瓶生产厂家_徐州明政玻璃制品有限公司 | 南京欧陆电气股份有限公司-风力发电机官网 | China plate rolling machine manufacturer,cone rolling machine-Saint Fighter | 好物生环保网、环保论坛 - 环保人的学习交流平台 | 整车VOC采样环境舱-甲醛VOC预处理舱-多舱法VOC检测环境仓-上海科绿特科技仪器有限公司 | 济南网站建设_济南网站制作_济南网站设计_济南网站建设公司_富库网络旗下模易宝_模板建站 | 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 深圳市源和塑胶电子有限公司-首页 | 臭氧实验装置_实验室臭氧发生器-北京同林臭氧装置网 | 捷码低代码平台 - 3D数字孪生_大数据可视化开发平台「免费体验」 | 通风气楼_通风天窗_屋顶风机-山东美创通风设备有限公司 | 内六角扳手「厂家」-温州市威豪五金工具有限公司 | 工业淬火油烟净化器,北京油烟净化器厂家,热处理油烟净化器-北京众鑫百科 | 挤出机_橡胶挤出机_塑料挤出机_胶片冷却机-河北伟源橡塑设备有限公司 | 韦伯电梯有限公司|