Uncaught TypeError:无法使用onclick将属性'innerHTML'设置为null

时间:2020-03-02 05:29:37

标签: onclick

我正在尝试将1更改为x。我收到一个错误。也许有一种更简单的方法。

<html>
<head>
    <title>Test</title>
</head>
<body>
    <div class="container">
        <div id="id1" onclick="changeText(this.id); this.onclick=null;">1</div>
    </div>
  <script src="javascript.js"></script>
</body>
</html>

Javascript:

function changeText(selectedId) {
  document.getElementById("selectedId").innerHTML = "x"
};

1 个答案:

答案 0 :(得分:1)

您正在尝试获取具有id="selectedId"且没有元素的元素。

删除JavaScript中的引号就可以了:

document.getElementById(selectedId).innerHTML = "x"
相关问题