GVKun编程网logo

Javascript Uncaught TypeError: cannot read "style" - 我该如何解决这个问题?(js cannot read property of undefined)

1

本文将为您提供关于JavascriptUncaughtTypeError:cannotread"style"-我该如何解决这个问题?的详细介绍,我们还将为您解释jscannotreadproperty

本文将为您提供关于Javascript Uncaught TypeError: cannot read "style" - 我该如何解决这个问题?的详细介绍,我们还将为您解释js cannot read property of undefined的相关知识,同时,我们还将为您提供关于Android webview vue网页 Uncaught TypeError: Cannot read property ‘getItem‘ of null、bug之bootstrap switch Uncaught TypeError: Cannot read property ''apply'' of undefined、cocos2d-js-----Uncaught TypeError: Cannot read property 'style' of null、EasyUI 分页加载 datagrid 时候报错 "Uncaught TypeError: Cannot read property ''length'' of undefine...的实用信息。

本文目录一览:

Javascript Uncaught TypeError: cannot read

Javascript Uncaught TypeError: cannot read "style" - 我该如何解决这个问题?(js cannot read property of undefined)

如何解决Javascript Uncaught TypeError: cannot read "style" - 我该如何解决这个问题?

我的控制台出现了这个 js 错误,但我似乎无法找出实际问题所在。我错过了什么吗?希望得到一些关于如何解决这个问题的帮助。我有另一个版本,所有名称都没有“移动”字样,而且似乎工作得很好。

这是错误:

scriptMobile.js:26 Uncaught TypeError: Cannot read property ''style'' of null
    at clearallMobile (scriptMobile.js:26)
    at SVGGElement.LaksevagFunctionMobile (scriptMobile.js:43)

根据错误,我在正确的行添加了注释:

JS:

function clearallMobile(){
  // Clear Laksevag  
  document.getElementById("LaksevagMobile").style.opacity = "1"; //THIS IS LINE 26
  document.getElementById("Laksevag-H-Mobile").style.opacity = "0";
  // Clear Fana
  document.getElementById("FanaMobile").style.opacity = "1";
  document.getElementById("Fana-H-Mobile").style.opacity = "0";
  document.getElementById("Fana-H-Mobile").style.zIndex = "0"
  // Clear Landas
  document.getElementById("LandasMobile").style.opacity = "1";
  document.getElementById("Landas-H-Mobile").style.opacity = "0";
  document.getElementById("Landas-H-Mobile").style.zIndex = "0";
  // Clear FyllingsDalen
  document.getElementById("FyllingDalenMobile").style.opacity = "1";
  document.getElementById("FyllingDalen-H-Mobile").style.opacity = "0";
}



function LaksevagFunctionMobile() {
  clearallMobile() //THIS IS LINE 43
  resetActiveMobile()
  lastSelectedMobile = "LaksevagMobile";
  document.getElementById("LaksevagMobile").style.opacity = "0";
  document.getElementById("Laksevag-H-Mobile").style.opacity = "1";
  route.innerHTML = LaksevagBox.route;
  distance.innerHTML = LaksevagBox.dist;
  time.innerHTML = LaksevagBox.time;
  otherinfo.innerHTML = LaksevagBox.info;

  //button toggles
  DetailsBtnMobile.addEventListener("click",LaksevagDetailsMobile);
  InfoBtnMobile.addEventListener("click",LaksevagInfoMobile);

  function LaksevagInfoMobile() {
    document.getElementById("info-innerMobile").style.opacity = "1"
    document.getElementById("details-innerMobile").style.opacity = "0"
    document.getElementById("laksevag-detMobile").style.opacity = "0"
  }

  function LaksevagDetailsMobile() {
    document.getElementById("info-innerMobile").style.opacity = "0"
    document.getElementById("details-innerMobile").style.opacity = "1"

    document.getElementById("laksevag-detMobile").style.opacity = "1"
    document.getElementById("fyllingDalen-detMobile").style.opacity = "0"
    document.getElementById("landas-detMobile").style.opacity = "0"
    document.getElementById("fana-detMobile").style.opacity = "0"
  }
} 

CSS:

#FanaMobile,#FyllingDalenMobile,#LaksevagMobile,#LandasMobile {
    opacity: 1;
}

#Fana-H-Mobile,#FyllingDalen-H-Mobile,#Laksevag-H-Mobile,#Landas-H-Mobile {
    opacity: 0;
}

#FanaNameMobile,#FyllingDalenNameMobile,#LaksevagNameMobile,#LandasNameMobile {
    cursor: pointer;
}

Android webview vue网页 Uncaught TypeError: Cannot read property ‘getItem‘ of null

Android webview vue网页 Uncaught TypeError: Cannot read property ‘getItem‘ of null

1.问题

在Android 的 webview 中 显示 vue 网页后,发现有个网址传参并没有实现正确的逻辑操作,在页面上显示的结果不一样。
测试网页传参,传参读取正确。测试逻辑运算及其赋值,正确。
发现问题:在网页打印反馈中发现报错:Uncaught TypeError: Cannot read property ‘getItem’ of null

2.原因

getItem是网页上调用localStorage或者sessionStorage在浏览器中读取储存数据时调用的方法。
现在该方法读取的为空,按网页的代码来看如果为空会走其他的方法,但是查看发现为空的情况也没实现,应该的是在读写浏览器的储存数据方面导致问题。查询该报错了解到,Android 的 webview 中默认禁止网页Dom缓存的,所以网页无法读写缓存数据,才导致该问题。(还有人抱怨说前端工程师写的代码不规范,前端可不背锅,iOS和网页都没该问题,话说,Android不也是前端么)。

3.解决

webview 中设置允许缓存数据。


//允许Dom缓存数据
webSettings.setDomStorageEnabled(true); 

4.反思

有问题还得先从调试窗口看起,把报错解决了,别自己臆想些问题原因。先解决了那个报错,岂不能节省很多时间。

bug之bootstrap switch Uncaught TypeError: Cannot read property ''apply'' of undefined

bug之bootstrap switch Uncaught TypeError: Cannot read property ''apply'' of undefined

<input type="checkbox" name="my-switch" checked>

js写以下代码,不然switch控件出不来

$("[name=''my-switch'']").bootstrapSwitch();

$(''[name="my-switch"]'').bootstrapSwitch({  

         onText:"启动",  

        offText:"停止",  

        onColor:"success",  

        offColor:"info",  

        size:"small",  

        onSwitchChange:function(event,state){  

            if(state==true){  

                $(this).val("1");  

            }else{  

                $(this).val("2");  

            }  

        }  

    })  

上述方法,没有亲测,大家可以试一试!

<divdata-on-label="开" data-off-label="关" id="toggle-state-switch" data-on="success" data-off="warning">
    <input id ="toggle" type="checkbox"  checked="checked"/>
</div>

function changeState(stateValue){
        if(stateValue==1){
            $(''#toggle-state-switch'').bootstrapSwitch(''setState'', true);
        }else{
            $(''#toggle-state-switch'').bootstrapSwitch(''setState'', false);
        }
}

使用上述的方法

$(''#toggle-state-switch'').bootstrapSwitch(''setState'', false); 是有问题的。

应该使用

$(''#toggle-state-switch'').bootstrapSwitch(''state'', true/false);

还有一个坑,就是在使用外面的div的id设置bootstrapSwitch的state,会多了节点,如下:

这并不是我们想要的结果,所以,设置外置div的值是不对的。应该取checkbox的id设置,也就是,如下:

$(''#toggle'').bootstrapSwitch(''state'', false);

对bootstrap switch进行赋值的时候,提示错误 
Uncaught TypeError: Cannot read property ''apply'' of undefined


        if (isUp==undefined || isUp == ''''){
            $(''#isUp'').bootstrapSwitch(''setState'',true);
        } else{
            $(''#isUp'').bootstrapSwitch(''setState'',isUp);
        }

详细错误信息如下图所示: 
 
我参考这个设置 使用jQuery获取Bootstrap Switch的值 
看来有问题,于是查看源码,根本没有setState方法,应该使用下面的方法:

if(stateValue==0){
            $(''#toggle'').bootstrapSwitch(''state'', true);
            console.log("是否启用的状态1:",stateValue);
        }else{
            console.log("是否启用的状态2:",stateValue);
            $(''#toggle'').bootstrapSwitch(''state'', false);
        }

cocos2d-js-----Uncaught TypeError: Cannot read property 'style' of null

cocos2d-js-----Uncaught TypeError: Cannot read property 'style' of null

原因很简单,就是因为你代码里面用到这个js文件,但是project.json里面没有添加到jsList里面。

比如我注释了Alert.json

<span>  "jsList" : [</span>
        "src/resource.js","src/app.js","src/BattleScene.js","src/CSVReader.js","src/Common.js","src/Level.js",//"src/Alert.js"
    ]
}

就报错了:


CCBoot.js:646 Uncaught TypeError: Cannot read property ''style'' of null

EasyUI 分页加载 datagrid 时候报错

EasyUI 分页加载 datagrid 时候报错 "Uncaught TypeError: Cannot read property ''length'' of undefine...

最近新做了一个模块,还是用的 easyui,但是出现了一个问题,研究了好久都没有找到解决问题的方法:

  情况如下:

    我在加载 datagrid 的时候,数据报错 “Uncaught TypeError: Cannot read property ''length'' of undefined”,最后知道了原因是 data 数据的问题,

    我的 data 数据是 {"Total":1,"Rows":[{ }] } 这种格式,

      这时候只需要在分页的代码里加入 var data = data.Rows; 即可解决问题,这个只针对上面数据格式的情况解决,具体代码如下:

    function pagerFilter(data){
  var data = data.Rows;// 新加的代码
  if (typeof data.length == ''number'' && typeof data.splice == ''function''){ // 判断数据是否是数组
  data = {
    total: data.length,
    rows: data
   };
  };
  var dg = $(this);
  var opts = dg.datagrid(''options'');
  var pager = dg.datagrid(''getPager'');
  pager.pagination({
  onSelectPage:function(pageNum, pageSize){
  opts.pageNumber = pageNum;
  opts.pageSize = pageSize;
  pager.pagination(''refresh'',{
  pageNumber:pageNum,
  pageSize:pageSize
 });
  dg.datagrid(''loadData'',data);
 }
});
if (!data.originalRows){
  data.originalRows = (data.rows);
}
 var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
   var end = start + parseInt(opts.pageSize);
   data.rows = (data.originalRows.slice(start, end));
   return data;
};
$(''#tt'').datagrid({loadFilter:pagerFilter});

 

【声明:本文为作者原创,未经允许不可转载!谢谢!】

今天关于Javascript Uncaught TypeError: cannot read "style" - 我该如何解决这个问题?js cannot read property of undefined的讲解已经结束,谢谢您的阅读,如果想了解更多关于Android webview vue网页 Uncaught TypeError: Cannot read property ‘getItem‘ of null、bug之bootstrap switch Uncaught TypeError: Cannot read property ''apply'' of undefined、cocos2d-js-----Uncaught TypeError: Cannot read property 'style' of null、EasyUI 分页加载 datagrid 时候报错 "Uncaught TypeError: Cannot read property ''length'' of undefine...的相关知识,请在本站搜索。

本文标签: