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;
       }
    
  • 相关阅读:
    结构型模式上
    创建型模式下
    创建型模式中
    创建型模式上
    设计模式总述
    Java中事件机制
    UI常用控件
    UITextField和UIViewConteoller
    UIScrollView 和 UIPageControl
    分栏视图控制器
  • 原文地址:https://www.cnblogs.com/pcblogs/p/13085137.html
Copyright © 2011-2022 走看看