未捕获的类型错误:不能设置为空值的特性“的innerHTML” [英] Uncaught TypeError: Cannot set property 'innerHTML' of null

查看:372
本文介绍了未捕获的类型错误:不能设置为空值的特性“的innerHTML”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的PhoneGap-1.4.1为Android酒吧code扫描应用程序。我需要定义一个数组 code [] ,可存储所有的酒吧code文本扫描,我需要定义一个变量氏/ code>充当一个计数器,得到由1每次扫描后递增,这样我可以储存详细 code [K] 。因此,这里是我的JavaScript文件,其中我已经定义了一个数组和一个变量counter previous。

I am making a barcode scanning app using phonegap-1.4.1 for android. I need to define a n 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]. So here is my previous javascript file where i have defined an array and an variable counter.

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

和这里是我的另外一个js文件我在哪里调用存储阵列,并打印成存储在阵列值ID myvalue1 从扫描获得​​的条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". myvalue1 is the barcode text obtained from the scan.

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

我一次又一次地呼唤吧code扫描功能。这就是为什么我使用一个数组存储律师code scanned.Here的数据是我扫描的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);
       });
  }

我收到错误,因为未捕获的类型错误:不能设置为空值的特性的innerHTML。我是新来的PhoneGap和编码。任何人都请帮我出这个问题。先谢谢了。

I am getting the error as 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.

推荐答案

您不必跟踪变量 K 。 使用下面的脚本您简单的管理你的所有的酒吧code值。

You don't need to keep track of variable k. You simple manage your all barcode values using below script.

请检查下面的jsfiddle ...运行它两次,你可以看到区别。

Please check below jsFiddle...Run it twice you can see difference.

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

这篇关于未捕获的类型错误:不能设置为空值的特性“的innerHTML”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆