Uncaught TypeError: Cannot read property 'style' of null in function

Uncaught TypeError: Cannot read property 'style' of null in function

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

我正在创建一个交互式页面来创建基于几何形状的图案,但我在添加新元素时遇到了问题,该元素具有创建新元素并将该元素添加到“基础”的功能div.然后,克隆基础 div 以创建模式。

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

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

我可以使用 javascript 指令附加许多元素(代码中有一些示例),但是当我将这些指令放在函数中时,代码不起作用并且出现错误:“未捕获的类型错误:无法读取 属性 'style' of null”

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

//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>

第二次单击创建按钮时出现错误。

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

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

参考:Node.appendChild