简体   繁体   English

未捕获的TypeError:无法将属性'innerHTML'设置为null

[英]Uncaught TypeError: Cannot set property 'innerHTML' of null

I am making a barcode scanning app using phonegap-1.4.1 for android. 我正在使用用于Android的phonegap-1.4.1制作条形码扫描应用程序。 I need to define an array code[] which can store all the barcode text scanned and i need to define a variable k which act as a counter which get incremented by 1 after every scan so that I can store detail in code[k] . 我需要定义一个数组code[] ,它可以存储所有扫描的条形码文本,并且我需要定义一个变量k ,该变量k作为计数器,每次扫描后递增1,以便可以将详细信息存储在code[k] So here is my previous javascript file where i have defined an array and an variable counter. 所以这是我以前的javascript文件,其中我定义了一个数组和一个变量计数器。

     localStorage["counter"]=0;
     var code = new Array(100);
     localStorage.setItem("code", JSON.stringify(code));

and here is my another js file where i am calling the stored array and printing the value stored in that array as "id" . 这是我的另一个js文件,我在其中调用存储的数组并将该数组中存储的值打印为"id" myvalue1 is the barcode text obtained from the scan. myvalue1是从扫描获得的条形码文本。

   var barcodeVal = localStorage.getItem("myvalue1");
   var test2 = localStorage.getItem("code");
   code = JSON.parse(test2);
   var k = parseInt(localStorage.getItem("counter"));
   code[k] = "code[k]";
   document.getElementById(code[k]).innerHTML = barcodeVal;
   k = k + 1;
   localStorage["counter"]=k;
   localStorage.setItem("code", JSON.stringify(code));

I am calling the barcode scan function again and again. 我一次又一次地调用条形码扫描功能。 Thats why I am using an array for storing the data of the Barcode scanned.Here is my scan js file as well which gives the value of myvalue1 这就是为什么我使用一个数组来存储扫描条形码的数据。这也是我的扫描js文件,它给出了myvalue1的值

 <script type="text/javascript">
  var scanCode = function () {
      window.plugins.barcodeScanner.scan(
          function (result) {

               alert("Scanned Code: " + result.text + ". Format: " + result.format + ". Cancelled: " + result.cancelled);
              localStorage.setItem("myvalue1", result.text);
              window.location.href = 'page5.html';

          }, function (error) {
              alert("Scan failed: " + error);
       });
  }

I am getting the error as Uncaught TypeError: Cannot set property 'innerHTML' of null . 我收到作为Uncaught TypeError: Cannot set property 'innerHTML' of null的错误Uncaught TypeError: Cannot set property 'innerHTML' of null I am new to phonegap and coding . 我是电话和编码的新手。 Anyone please help me out with the issue. 有人请帮我解决这个问题。 Thanks in advance. 提前致谢。

You don't need to keep track of variable k . 您无需跟踪变量k You simple manage your all barcode values using below script. 您可以使用以下脚本简单地管理所有条形码值。

Please check below jsFiddle...Run it twice you can see difference. 请在下面的jsFiddle中检查...运行两次,可以看到区别。

JSFiddle 的jsfiddle

<script>
function SaveDataToLocalStorage(barcodeValue)
{

    var oldItems = JSON.parse(localStorage.getItem('barcodes')) || [];
    var newItem = {
        'barcode': barcodeValue
    };
    oldItems.push(newItem);
    localStorage.setItem('barcodes', JSON.stringify(oldItems));

}
SaveDataToLocalStorage("123456879FFGG");
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的Typeerror:无法将属性innerHTML设置为null - Uncaught Typeerror: Cannot set property innerHTML of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法设置null的属性&#39;innerHTML&#39; - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法在phonegap中将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null in phonegap 未捕获的TypeError:无法使用for循环将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null using a for loop 未捕获的TypeError:无法在第34行中将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null in line 34 Uncaught TypeError:无法在JS创建的div上将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null on JS created div 未被捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null [VueJs] - Uncaught TypeError: Cannot set property 'innerHTML' of null [VueJs] 未捕获(承诺)TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught (in promise) TypeError: Cannot set property 'innerHTML' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM