首页 > 解决方案 > 未捕获的类型错误:无法在函数中读取 null 的属性“样式”

问题描述

我已经看到了很多关于这个错误的问题,但是我没有找到任何问题的答案。

我正在创建一个交互式页面来创建基于几何形状的模式,并且我在使用创建新元素并将元素添加到“基础”div 的函数中添加新元素时遇到问题。然后,克隆基本 div 以创建模式。

在 HTML 中,我创建了一个颜色选择器以在创建元素之前预选颜色,以及一个包含创建新元素并将新元素附加到基本 div 的函数的按钮。

同样在 HTML 中,我使用“display:none”样式创建了 SVG 形状,当我使用 JS 检索它们时,我只需将其更改为“display:block”。

我可以使用 javascript 指令附加许多元素(代码中有一些示例),但是当我将这些指令放在函数中时,代码不起作用,并且出现错误:“Uncaught TypeError: Cannot read property 'style'为空”</p>

这是我的代码,我添加了注释以便于阅读。希望您能够帮助我。

//Global variables
var picker = document.getElementById('picker');
var color = picker.value;


//Base elements
var myRow = document.getElementById("row");
var myDiv = document.createElement("base");
myDiv.className = 'base';


// Retrieves the 'out_circle' SVG from HTML and defines properties
var innerDiv = document.getElementById("out_circle");
innerDiv.style.display = "block";
innerDiv.style.fill = "#3c00ff";
innerDiv.style.position = "absolute";
// Adds the element to the base div
myDiv.appendChild(innerDiv);

// Retrieves the 'sqr_small' SVG from HTML and defines properties
var innerDiv = document.getElementById("sqr_small");
innerDiv.style.display = "block";
innerDiv.style.fill = "purple";
innerDiv.style.position = "absolute";
innerDiv.style.margin = "5";
// Adds the element to the base div
myDiv.appendChild(innerDiv);

// Retrieves the 'diamond' SVG from HTML and defines properties
var innerDiv = document.getElementById("diamond");
innerDiv.style.display = "block";
innerDiv.style.fill = "green";
innerDiv.style.position = "absolute";
// Adds the element to the base div
myDiv.appendChild(innerDiv);

// Retrieves the 'slash_right' SVG from HTML and defines properties
var innerDiv = document.getElementById("slash_right");
innerDiv.style.display = "block";
innerDiv.style.fill = "orange";
innerDiv.style.position = "absolute";
// Adds the element to the base div
myDiv.appendChild(innerDiv);


//*****NOT WORKING FUNCTION*****
function create_Lslash() {
  // Retrieves the 'slash_left' SVG from HTML and defines properties
  var innerDiv = document.getElementById("slash_left");
  innerDiv.style.display = "block";
  innerDiv.style.fill = color;
  innerDiv.style.position = "absolute";
  // Adds the element to base div
  myDiv.appendChild(innerDiv);
}



// Clones the div to create a row
var i = 0;
while (i < 10) {
  myRow.appendChild(myDiv.cloneNode(true));
  i++;
}

//Clones the row to create a grid
var i = 0;
while (i < 10) {
  document.body.appendChild(myRow.cloneNode(true));
  i++;
}
/* a minimalist set of CSS resets */


/* default to border-box */

html {
  box-sizing: border-box;
  font-size: 16px;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}


/* adjust typography defaults */

body {
  margin: 1rem;
  font-family: sans-serif;
  line-height: 1.5;
}

.base {
  display: inline-block;
  height: 50px;
  width: 50px;
  margin: 0px;
  padding: 0px;
  position: relative;
}

.row {
  display: block;
  height: 50px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="icon" href="https://glitch.com/favicon.ico" />
  <title>Patterns</title>
  <!-- import the webpage's stylesheet -->
  <link rel="stylesheet" href="/style.css" />
  <!-- import the webpage's javascript file -->
  <script src="/script.js" defer></script>
</head>

<body>
  <!-- Color picker  -->
  <div style="padding:40px 0 0 0;">
    <input type="color" id="picker" value="#0000ff">
  </div>
  <!-- "Create" button  -->
  <div style="padding:40px 0 40px 0;">
    <button id="creator" onclick="create_Lslash()">Create</button>
  </div>
  <div id="row" class="row"></div>
  <!-- Elements to append  -->
  <div>
    <!-- Diamond  -->
    <svg id="diamond" x="0px" y="0px" width="50px" height="50px" viewbox="0 0 50 50" style="enable-background:new 0 0 50 50; display:none;" xml:space="preserve">
        <style type="text/css">
        </style>
        <rect x="7.32" y="7.32" transform="matrix(0.7071 -0.7071 0.7071 0.7071 -10.3553 25)" class="st0" width="35.36" height="35.36"/>
      </svg>
    <!-- Out_circle  -->
    <svg id="out_circle" x="0px" y="0px" width="50px" height="50px" viewbox="0 0 50 50" style="enable-background:new 0 0 50 50; display:none;" xml:space="preserve">
        <style type="text/css">
        </style>
        <defs>
        </defs>
        <g>
          <path class="st0" d="M25,50h25V25C50,38.81,38.81,50,25,50z"/>
          <path class="st0" d="M25,0c13.81,0,25,11.19,25,25V0H25z"/>
          <path class="st0" d="M0,25v25h25C11.19,50,0,38.81,0,25z"/>
          <path class="st0" d="M25,0H0v25C0,11.19,11.19,0,25,0z"/>
        </g>
      </svg>
    <!-- Small square  -->
    <svg id="sqr_small" x="0px" y="0px" width="40px" height="40px" viewbox="0 0 40 40" style="enable-background:new 0 0 40 40; display:none;" xml:space="preserve">
        <defs>
        </defs>
        <rect width="40" height="40"/>
      </svg>
    <!-- Right slash  -->
    <svg id="slash_right" x="0px" y="0px" width="50px" height="50px" viewbox="0 0 50 50" style="enable-background:new 0 0 50 50; display:none;" xml:space="preserve">
          <defs>
          </defs>
          <g>
              <polygon class="st0" points="6.36,0 0,0 0,6.36 43.64,50 50,50 50,43.64    " />
          </g>
      </svg>
    <!-- Left slash  -->
    <svg id="slash_left" x="0px" y="0px" width="50px" height="50px" viewbox="0 0 50 50" style="enable-background:new 0 0 50 50; display:none;" xml:space="preserve">
      <defs>
      </defs>
      <g>
        <polygon class="st0" points="0,43.64 0,50 6.36,50 50,6.36 50,0 43.64,0  "/>
      </g>
      </svg>
  </div>
</body>

</html>

标签: javascripthtmlcsssvgappendchild

解决方案


第二次单击“创建”按钮时会出现该错误。

该函数create_Lslash()调用myDiv.appendChild(innerDiv). 因为innerDiv这里是一个现有元素svg#slash_left,所以它从当前位置移动到新位置(的子元素myDiv)。但是在此之后您不会追加myDiv回 html 文档。

因此,当您再次单击创建并评估为svg#slash_left时,在文档中找不到。var innerDiv = document.getElementById("slash_left");null

参考:Node.appendChild


推荐阅读