zoukankan      html  css  js  c++  java
  • Uncaught TypeError :Connot read property 'getAttribute' of undefined


    这是我在前端写获取button属性的时候遇到的错误,具体情况是前端接收后端传过来的数据,循环打印值传递到button的value属性,但是有的产品是没有这个属性值的,就导致未定义错误,那么就需要在原有的基础上加一个判断,如果button存在(即value值可以被获取到),那么就将通过document.getElementsByClassName('active attr')[0].getAttribute("value");获取button中的value值,如果不存在(也就是这个未定义错误),那么赋值为null,那么问题来了,怎么判断未定义呢?其实很简单,使用typeof()判断一下就好:

     var $a = document.getElementsByClassName('active attr')[0];
       if(typeof($a) != "undefined"){
          var $attr_id = document.getElementsByClassName('active attr')[0].getAttribute("value");
          }else{
          $a = null;
       }
    
  • 相关阅读:
    UVa 727
    UVa 11495
    UVa 299
    UVa 10194
    UVa 146
    10025
    Tug of War POJ 2576 DP(类似背包)
    Problem A: Freckles UVA 10034 裸生成树
    UVA 562
    CF DIV 2 206 C. Vasya and Robot
  • 原文地址:https://www.cnblogs.com/pcblogs/p/13085137.html
Copyright © 2011-2022 走看看