[英]Html javascript getattribute not working
I'm stuck on this for a while now and i don't know whats wrong. 我现在坚持了一段时间,我不知道怎么了。 My getAttribute is always returning undefined.. I've tried a lot ,i messed with value and classnames but it always returns undefined.
我的getAttribute总是返回undefined。。我已经尝试了很多,我弄乱了值和类名,但是它总是返回undefined。 please help someone
请帮助某人
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="resources/css/Home.css" rel="stylesheet"/>
</head>
<body>
<div id="floatleft">
<img class="ploegen.html" onclick="GoTo(this)" onmouseleave="leave(this)" onmouseover="hover(this)" src="resources/ploegenhome.jpg">
<img onclick="GoTo(x)" onmouseleave="leave(this)" onmouseover="hover(this)" src="resources/stadionhome.jpg">
<img onclick="GoTo(x)" onmouseleave="leave(this)" onmouseover="hover(this)" src="resources/opstellinghome.png">
<img onclick="GoTo(x)" onmouseleave="leave(this)" onmouseover="hover(this)" src="resources/simulatiehome.jpg">
</div>
<img src="resources/logo_wk_2018.png" id="logowk">
<div id="tekstjes">
<ul>
<li><a>Ploegen</a></li>
<li><a>Stadions</a></li>
<li><a>Opstelling</a></li>
<li><a>Simulatie</a></li>
<ul/>
</div>
<p id="lol"></p>
<script>
function GoTo(x){
var y = x.getAttribute(className);
document.getElementById("lol").innerText = y;
}
function leave(x){
x.style.opacity = 0.5;
}
function hover(x){
x.style.opacity = 1;
var y = x.getAttribute(className);
document.getElementById("lol").innerText = y;
}
</script>
</body>
</html>
ClassName
in your code. ClassName
变量。 x
in your onClick function for goTo instead of this
x
,而不是this
<ul>
tag you used <ul/>
instead of </ul>
to close the tag. <ul>
标记的末尾使用<ul/>
而不是</ul>
来关闭标记。 function GoTo(x){ var y = x.getAttribute("class"); document.getElementById("lol").innerText = y; } function leave(x){ x.style.opacity = 0.5; } function hover(x){ x.style.opacity = 1; var y = x.getAttribute('class'); document.getElementById("lol").innerText = y; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="resources/css/Home.css" rel="stylesheet"/> </head> <body> <div id="floatleft"> <img class="ploegen.html" onclick="GoTo(this)" onmouseleave="leave(this)" onmouseover="hover(this)" src="resources/ploegenhome.jpg"> <img onclick="GoTo(this)" onmouseleave="leave(this)" onmouseover="hover(this)" src="resources/stadionhome.jpg"> <img onclick="GoTo(this)" onmouseleave="leave(this)" onmouseover="hover(this)" src="resources/opstellinghome.png"> <img onclick="GoTo(this)" onmouseleave="leave(this)" onmouseover="hover(this)" src="resources/simulatiehome.jpg"> </div> <img src="resources/logo_wk_2018.png" id="logowk"> <div id="tekstjes"> <ul> <li><a>Ploegen</a></li> <li><a>Stadions</a></li> <li><a>Opstelling</a></li> <li><a>Simulatie</a></li> </ul> </div> <p id="lol"></p> </body> </html>
Thanks to @RogerC for who spotted a mistake in the code that I thought I had fixed :) 感谢@RogerC谁发现了我认为自己已解决的代码错误:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.