繁体   English   中英

为什么此javascript会导致Safari浏览器崩溃,而不是firefox?

[英]why does this javascript crash safari but not firefox?

我有一个窗格,其中包含一组由javascript生成的表,每个表具有唯一的ID和4个单元格,并且我使用以下Javascript代码为其中一个表设置背景色。 它可以在Firefox中正常工作,但是在首次尝试设置背景颜色(在if语句中)时会导致Safari崩溃。 有什么想法吗?

<script language='Javascript'>
  function colortree(source) {
    var el=parent.frames['tree-pane'].document.getElementsByTagName('table');
    for (var i=0;i<el.length;i++) {
        var id = el[i].id;
        if (id) {
           var cell = el[i].getElementsByTagName('td')[3];
           if (id == source) { cell.style.backgroundColor = 'yellow' }
           else { cell.style.backgroundColor = 'white' };
        }
    }
    return false;
  }
</script>

如果有可能不存在数组索引,则应始终测试它们是否存在

例如

<script language='Javascript'>
  function colortree(source) {
    var cells, cell, id;
    var el=parent.frames['tree-pane'].document.getElementsByTagName('table');
    for (var i=0;i<el.length;i++) {
        id = el[i].id;
        if (id) {
           cells = el[i].getElementsByTagName('td');
           if (cells[3]) {
               cell = cells[3];
               if (id == source) { cell.style.backgroundColor = 'yellow' }
               else { cell.style.backgroundColor = 'white' };
           }
        }
    }
    return false;
  }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM