【javascript】异常错误

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <script type="text/javascript">
    window.onload = function(){
        var row = document.createElement("tr");
        document.getElementById("tbody").appendChild(row);
    };
  </script>
</head>
<body>
<table>
</table>
    <tbody id="tbody">
    </tbody>
</body>
</html>

//每个浏览器会报不同的错误(由错误的标签嵌套引起,需要把tbody放在table里)

+| IE 6,7,8   | 意外地调用了方法或属性访问。                                                                   | 0      |
+| IE 9       | 无法获取属性“appendChild”的值: 对象为 null 或未定义                                          | 0      |
+| IE 10      | 无法获取未定义或 null 引用的属性“appendChild”                                                | 0      |
+| Chrome 17  | Uncaught TypeError: Cannot call method 'appendChild' of null                                   |        |
+| Safari 6   | TypeError: 'null' is not an object (evaluating 'document.getElementById("tbody").appendChild') |        |
+| Firefox 21 | TypeError: document.getElementById(...) is null                                                |        |
+| Opera 12   | Uncaught exception: TypeError: Cannot convert 'document.getElementById("tbody")' to object     |        |

二 、 li 节点设置 value 属性(可以设置别的属性代替)

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
<script type="text/javascript">
  window.onload = function(){
    document.getElementById("btn-test").onclick = function(){
      var ul = document.createElement("ul");
      var li = document.createElement("li");
      //下面两行代码会触发异常
     // li.value = "11";
      li.setAttribute("value","11");  
      ul.appendChild(li);
      document.body.appendChild(ul);
    };
  };
</script>
</head>
<body>
<button id="btn-test">点击开始测试</button>
</body>
</html>

// 已知 IE6, IE7 会导致浏览器崩溃

 三、1.9版本不支持msie 会发生 异常错误

<!DOCTYPE html>
<html>
<head>

  <meta charset="utf-8">
  <title>JS Bin</title>
  <style type ='text/css'>
    .ovy{ position:absolute; top:50px; left:10px;background-color:red; width:100px; height:100px;}
  </style>
  <script src="jquery-1.9.0.js"></script>
</head>
<body>
  <div class='ovy'>1111</div>
  <script type='text/javascript'>
  $(function(){
          setTimeout(function(){
        //if(navigator.userAgent.indexOf('MSIE') != -1){
        if($.browser.MSIE){
            $('.ovy').animate({top:"0"})
        }
       },1000);

  })

  </script>
</body>
</html>

//  Cannot read property 'MSIE' of undefined  (是因为jquery 1.9不支持$.browser.msie)

 四、参数无效

  <script type='text/javascript'>
   window.onload=function(){
       var b ='aaa'; //var b;
          var elem = document.createElement('div');
          elem.style.width = b + 'px'; 
          document.body.appendChild(elem);
   }

  </script>

//IE下会提示 参数无效   b为undefined\NaN\-1

源文来自:http://blog.hotoo.me/post/javascript-exceptions-archives.html

posted @ 2014-08-15 11:31  Western Journey  阅读(910)  评论(0编辑  收藏  举报