-2 html javascript css
我看过各种网站,但没有帮助。对我来说,一切似乎都是正确的,但我无法
document.getElementByClassName("first").style.display="none";
Run Code Online (Sandbox Code Playgroud)
不管我尝试多少次,我都能在JS上收到相同的错误消息;
错误:未定义'文档'。[no-undef]
尝试定义“文档”部分,但没有帮助。也许是我在尝试连接外部文件夹之间的连接不正确,但没有做任何更改
HTML:
<html>
<head>
<script src="JS.js"></script>
</head>
<body>
<div class="first">
<p>Hello and welcome to my first page<br>
in this page I will test out new think<br>
and see what works and what doesn't<br>
</p>
</div>
<button type="button" onclick="myFunction">Click me</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
JS:
function myFunction(){
document.getElementsByClassName("first").style.display="none";
Run Code Online (Sandbox Code Playgroud)
该按钮应该是从“第一”清除所有样式。我更改了许多结果,并且代码没有任何变化,只是相同的错误一遍又一遍地重复。
document.getElementByClassName()
返回元素数组,因此您需要要定位的元素的索引。
您应该通过调用该函数myFunction()
并添加[0]
到getElementsByClassName
以获取特定元素。
function myFunction() {
document.getElementsByClassName("first")[0].style.display="none";
}
Run Code Online (Sandbox Code Playgroud)
<html>
<head>
<script src="JS.js"></script>
</head>
<body>
<div class="first">
<p>Hello and welcome to my first page<br>
in this page I will test out new think<br>
and see what works and what doesn't<br>
</p>
</div>
<button type="button" onclick="myFunction()">Click me</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
71 次 |
最近记录: |