我正在尝试将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"
};
答案 0 :(得分:1)
您正在尝试获取具有id="selectedId"
且没有元素的元素。
删除JavaScript中的引号就可以了:
document.getElementById(selectedId).innerHTML = "x"